Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
   *
   * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
   * Other names may be trademarks of their respective owners.
   *
   * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common
  * Development and Distribution License("CDDL") (collectively, the
  * "License"). You may not use this file except in compliance with the
  * License. You can obtain a copy of the License at
  * http://www.netbeans.org/cddl-gplv2.html
  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  * specific language governing permissions and limitations under the
  * License.  When distributing the software, include this License Header
  * Notice in each file and include the License file at
  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Oracle in the GPL Version 2 section of the License file that
  * accompanied this code. If applicable, add the following below the
  * License Header, with the fields enclosed by brackets [] replaced by
  * your own identifying information:
  * "Portions Copyrighted [year] [name of copyright owner]"
  *
  * If you wish your version of this file to be governed by only the CDDL
  * or only the GPL Version 2, indicate your decision by adding
  * "[Contributor] elects to include this software in this distribution
  * under the [CDDL or GPL Version 2] license." If you do not indicate a
  * single choice of license, a recipient has the option to distribute
  * your version of this file under either the CDDL, the GPL Version 2 or
  * to extend the choice of license to its licensees as provided above.
  * However, if you add GPL Version 2 code and therefore, elected the GPL
  * Version 2 license, then the option applies only if the new code is
  * made subject to such option by the copyright holder.
  *
  * Contributor(s):
  *
  * Portions Copyrighted 2012 Sun Microsystems, Inc.
  */
 package org.netbeans.modules.web.inspect.webkit.ui;
 
 import java.net.URL;
 import java.util.List;
WebKit-based CSS Styles view.

Author(s):
Jan Stola
public class CSSStylesPanel extends JPanel implements PageModel.CSSStylesView {
    
Request processor used by this class.
    private static final RequestProcessor RP = new RequestProcessor(CSSStylesPanel.class);
    
The default instance of this class.
    private static final CSSStylesPanel DEFAULT = new CSSStylesPanel();
    
Selection section of CSS Styles view.
The current inspected page.
    WebKitPageModel pageModel;
    
Lookup of this panel.
    private CSSStylesLookup lookup = new CSSStylesLookup();
    
Node lookup of this panel.
    private CSSStylesNodeLookup nodeLookup = new CSSStylesNodeLookup();
    
Lookup result with rules selected in the panel.
Creates a new CSSStylesPanel.
    private CSSStylesPanel() {
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(400,400));
        PageInspectorImpl.getDefault().addPropertyChangeListener(getListener());
         = .lookupResult(Rule.class);
        updatePageModel();
    }

    
Returns the default instance of this class.

Returns:
the default instance of this class.
    public static CSSStylesPanel getDefault() {
        return ;
    }

    
Listener for various events this instance is interested in.
    private Listener listener;
    
Returns the listener.

Returns:
the listener.
    private Listener getListener() {
        if ( == null) {
             = new Listener();
        }
        return ;
    }

    
Updates the panel to match the currently inspected page.
    public final void updatePageModel() {
        PageModel page = PageInspectorImpl.getDefault().getPage();
        if ( == page) {
            return;
        }
        if ( != null) {
            .getWebKit().getCSS().removeListener(getListener());
        }
        if (page instanceof WebKitPageModel) {
             = (WebKitPageModel)page;
        } else {
             = null;
        }
        if ( != null) {
            .getWebKit().getCSS().addListener(getListener());
        }
        updateContent(false);
    }

    
Updates the content of this panel.

Parameters:
keepSelection if true then an attempt to keep the current selection is made, otherwise the selection is cleared.
    void updateContent(boolean keepSelection) {
        try {
             = keepSelection;
            .setPageModel();
            .updateContent(keepSelection);
        } finally {
            // Ugly hack that ensures that contentUpdateInProgress
            // is not set to false before the update of Document
            // and Selection panes is finished
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    ..post(new Runnable() {
                        @Override
                        public void run() {
                            ..post(new Runnable() {
                                @Override
                                public void run() {
                                    EventQueue.invokeLater(new Runnable() {
                                        @Override
                                        public void run() {
                                             = false;
                                            updateRulesEditor(.allInstances());
                                        }
                                    });
                                }
                            });
                        }
                    });
                }
            });
        }
    }

    
Determines whether the content update is in progress.
    boolean contentUpdateInProgress;

    
Updates the rules editor window to show information about the selected rule.

Parameters:
rules rules selected in this panel.
    void updateRulesEditor(final Collection<? extends Rulerules) {
        .post(new Runnable() {
            @Override
            public void run() {
                if ( != null) {
                    String selector = null;
                    if  (rules.size() == 1) {
                        Rule rule = rules.iterator().next();
                        selector = rule.getSelector();
                    }
                    .setSelectedSelector(selector);
                }
            }
        });
        final RuleInfo ruleInfo = (rules.size() == 1) ? .lookup(RuleInfo.class) : null;
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                CssStylesTC ruleEditor = (CssStylesTC)WindowManager.getDefault().findTopComponent(.);
                final RuleEditorController controller = ruleEditor.getRuleEditorController();
                .post(new Runnable() {
                    @Override
                    public void run() {
                        if (rules.size() == 1) {
                            Rule rule = rules.iterator().next();
                            String resourceName = rule.getSourceURL();
                            Project project = null;
                            if ( != null) {
                                project = .getProject();
                            }
                            FileObject fob = new Resource(projectresourceName).toFileObject();
                            if (fob == null) {
                                controller.setNoRuleState();
                            } else {
                                try {
                                    Source source = Source.create(fob);
                                    ParserManager.parse(Collections.singleton(source), new RuleEditorTask(ruleruleInfocontroller));
                                } catch (ParseException ex) {
                                    Logger.getLogger(CSSStylesPanel.class.getName()).log(.nullex);
                                }
                            }
                        } else {
                            controller.setNoRuleState();
                        }
                    }
                });
            }
        });
    }
    
    @Override
    public JComponent getView() {
        return this;
    }
    @Override
    public Lookup getLookup() {
        return ;
    }
    @Override
    public void activated() {
    }
    @Override
    public void deactivated() {
    }

    
Listener for various events important for CSSStylesPanel.
    class Listener implements PropertyChangeListenerLookupListenerCSS.Listener {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            String propName = evt.getPropertyName();
            if (..equals(propName)) {
                updatePageModel();
            } else if (..equals(propName)) {
                updateContent(false);
            }
        }
        @Override
        public void resultChanged(LookupEvent ev) {
            Collection<? extends Rulerules = .allInstances();
            // Trying to avoid unwanted flashing of Rule Editor
            if (!) {
                updateRulesEditor(rules);
            }
        }
        @Override
        public void mediaQueryResultChanged() {
            updateContentInRP();
        }
        @Override
        public void styleSheetChanged(String styleSheetId) {
            updateContentInRP();
        }

        
Invokes updateContent() in a request processor.
        private void updateContentInRP() {
            .post(new Runnable() {
                @Override
                public void run() {
                    updateContent(true);
                }
            });
        }
    }

    
Lookup of CSSStylesPanel.
    static class CSSStylesLookup extends ProxyLookup {
        protected final void updateLookup(Lookup lookup) {
            if (lookup == null) {
                setLookups();
            } else {
                setLookups(lookup);
            }
        }
    }

    
User task that updates the rules editor window (to show the specified rule).
    static class RuleEditorTask extends UserTask {
        
Rule to show in the rules editor.
        private Rule rule;
        
Additional rule information.
        private RuleInfo ruleInfo;
        
Controller of the rule editor where the rule should be shown.
        private RuleEditorController controller;

        
Creates a new RuleEditorTask.

Parameters:
rule rule to show in the rules editor.
ruleInfo additional rule information.
controller controller of the rule editor where the rule should be shown.
        RuleEditorTask(Rule ruleRuleInfo ruleInfoRuleEditorController controller) {
            this. = rule;
            this. = ruleInfo;
            this. = controller;
        }

        
Determines whether the property with the specified name and value has been parsed without problems.

Parameters:
propertyName name of the property to check.
propertyValue value of the property to check.
Returns:
true if the property has been parsed without problems, returns false otherwise.
        private boolean isParsedOk(String propertyNameString propertyValue) {
            for (org.netbeans.modules.web.webkit.debugging.api.css.Property property : .getStyle().getProperties()) {
                if (!property.isParsedOk()
                        && property.getName().equals(propertyName)
                        && property.getValue().equals(propertyValue)) {
                    return false;
                }
            }
            return true;
        }
        @Override
        public void run(ResultIterator resultIteratorthrows Exception {
            final boolean[] found = new boolean[1];
            for (final CssParserResult result : Utilities.cssParserResults(resultIterator)) {
                final Model sourceModel = Model.getModel(result);
                sourceModel.runReadTask(new Model.ModelTask() {
                    @Override
                    public void run(StyleSheet styleSheet) {
                        org.netbeans.modules.css.model.api.Rule modelRule = Utilities.findRuleInStyleSheet(sourceModelstyleSheet);
                        if (modelRule != null) {
                            .setModel(sourceModel);
                            .setRule(modelRule);
                            if ( != null) {
                                List<Stringactive = new ArrayList<String>();
                                List<Declarationdeclarations = modelRule.getDeclarations().getDeclarations();
                                for (int i=declarations.size()-1; i>=0; i--) {
                                    Declaration declaration = declarations.get(i);
                                    Property property = declaration.getProperty();
                                    String propertyName = property.getContent().toString().trim();
                                    PropertyValue propertyValue = declaration.getPropertyValue();
                                    Expression expression = propertyValue.getExpression();
                                    String value = expression.getContent().toString().trim();
                                    if (isIEHackIgnoredByWebKit(propertyresult.getSnapshot())) {
                                        .setDeclarationInfo(declaration.);
                                    } else if (isParsedOk(propertyNamevalue)) {
                                        if (!.isInherited() || CSSUtils.isInheritedProperty(propertyName)) {
                                            if (.isOverriden(propertyName) || active.contains(propertyName)) {
                                                .setDeclarationInfo(declaration.);
                                            } else {
                                                active.add(propertyName);
                                            }
                                        } else {
                                            // Inherited rule but a property that is not inherited
                                            .setDeclarationInfo(declaration.);
                                        }
                                    } else {
                                        .setDeclarationInfo(declaration.);
                                    }
                                }
                            }
                            found[0] = true;
                        }
                    }
                });
                if (found[0]) {
                    break;
                }
            }
            if (!found[0]) {
                .setNoRuleState();
            }
        }

        
Determines whether the given property uses star or underscore hack to affect Internet Explorer only.

Parameters:
property property to check.
snapshot snapshot of the styleSheet.
Returns:
true when the property uses star or underscore hack.
        private boolean isIEHackIgnoredByWebKit(Property propertySnapshot snapshot) {
            String styleSheetText = snapshot.getText().toString();
            int startOffset = property.getStartOffset();
            char c = styleSheetText.charAt(startOffset-1);
            return (c == '_' || c == '*');
        }
    }

    
Node lookup of this panel.
    static class CSSStylesNodeLookup extends ProxyLookup {

        
Updates the lookup.

Parameters:
pageModel current page model.
        void setPageModel(final WebKitPageModel pageModel) {
            if (EventQueue.isDispatchThread()) {
                .post(new Runnable() {
                    @Override
                    public void run() {
                        setPageModel(pageModel);
                    }
                });
                return;
            }
            URL url = null;
            FileObject fob = null;
            Project project = null;
            DataObject dob = null;
            if (pageModel != null) {
                try {
                    project = pageModel.getProject();
                    if (project != null) {
                        String documentURL = pageModel.getDocumentURL();
                        url = new URL(documentURL);
                        fob = ServerURLMapping.fromServer(projecturl);
                        if (fob != null) {
                            dob = DataObject.find(fob);
                        }
                    }
                } catch (MalformedURLException ex) {
                } catch (DataObjectNotFoundException dnfex) {}
            }
            Lookup lkp;
            if (dob == null) {
                lkp = .;
            } else {
                lkp = Lookups.fixed(urlfobprojectdob);
            }
            Node node = new AbstractNode(.lkp);
            setLookups(Lookups.singleton(node));
        }
        
    }
New to GrepCode? Check out our FAQ X