apps/scenebuilder/SceneBuilderKit/src/com/oracle/javafx/scenebuilder/kit/editor/panel/css/CssUtils.java

Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization


 366 //    public static final String HIERARCHY_CSS = Utils.getResourceURL(Frame.class, "css_stylesheets/Hierarchy.css"); //NOI18N
 367 //    public static final String INSPECTOR_CSS = Utils.getResourceURL(Frame.class, "css_stylesheets/Inspector.css"); //NOI18N
 368 //    public static final String CSS_VIEWER_CSS = Utils.getResourceURL(Frame.class, "css_stylesheets/CssViewer.css"); //NOI18N
 369 //
 370 //    public static final String POPUP_CSS = Utils.getResourceURL(Frame.class, "css_stylesheets/Popup.css"); //NOI18N
 371 //    
 372 //    public static final String SCENE_BUILDER_THEME_STYLECLASS = "SCENE_BUILDER_THEME"; //NOI18N
 373 //    private static final String SCENE_BUILDER_WIN_FONT_STYLECLASS = "SCENE_BUILDER_WIN_FONT"; //NOI18N
 374 //    private static final String SCENE_BUILDER_WINXP_FONT_STYLECLASS = "SCENE_BUILDER_WINXP_FONT"; //NOI18N
 375 //    
 376 //    public static final String CONTENT_AREA_ID = "JFX_SB_ContentArea"; //NOI18N
 377 //    private static final String ROOT_STYLECLASS = "root"; //NOI18N
 378 //    private static Stylesheet STYLE_SHEET_TOOL_CSS = null;
 379 //    private static final Map<Class<?>, Set<String>> ALTERNATE_STYLECLASSES = new HashMap<>();
 380 //
 381 //    static Set<FileChooser.ExtensionFilter> SS_EXTENSIONS =
 382 //            Collections.singleton(new FileChooser.ExtensionFilter(Utils.getI18N().getString("popup.style.sheets"), "*.css", "*.bss")); //NOI18N
 383 //    
 384 //    static {
 385 //        try {
 386 //            STYLE_SHEET_TOOL_CSS = CSSParser.getInstance().parse(new URL(TOOL_ROOT_CSS));
 387 //        } catch (IOException ex) {
 388 //            Utils.println("Failed to parse " + TOOL_ROOT_CSS, ex); //NOI18N
 389 //        }
 390 //        Set<String> alternates = new HashSet<>();
 391 //        alternates.add("floating"); //NOI18N
 392 //        ALTERNATE_STYLECLASSES.put(TabPane.class, alternates);
 393 //    }
 394 //    //Properties that are impacting CSS/Pages.
 395 //    private static final Set<Prop> CSS_IMPACT = 
 396 //            SetBuilder.<Prop>make().
 397 //            add(PropUtils.Parent_STYLESHEETS).
 398 //            add(PropUtils.Node_ID).add(PropUtils.Styleable_STYLE_CLASS).add(PropUtils.Node_STYLE).
 399 //            add(PropUtils.Tab_ID).add(PropUtils.Tab_STYLE).
 400 //            add(PropUtils.PopupControl_ID).add(PropUtils.PopupControl_STYLE).
 401 //            add(PropUtils.Menu_ITEMS).buildUnmodifiable();
 402 //    private static final WeakIdentityHashMap<Parent, List<StyleClasses>> STYLECLASSES_SETS = WeakIdentityHashMap.make();
 403 //    
 404 //    public static void trackNode(final Node node) {
 405 //        attachMapToNode(node);
 406 //    }


 438 ////    public static void loadAuthorCssFiles(Frame frame) {
 439 ////        Parent contentParent = getParent(frame);
 440 ////        final Project project = frame.getProject();
 441 ////        try {
 442 ////            clearAuthorStyleSheets(contentParent);
 443 ////            for (Css css : project.getScreenData().getAuthorCss()) {
 444 ////                if (css.isActive()) {
 445 ////                    URL u = css.getFile().toURI().toURL();
 446 ////                    loadAuthorCss(frame, contentParent, u);
 447 ////                }
 448 ////            }
 449 ////        } catch (Exception ex) {
 450 ////            frame.printWarning("messagebar.cannot.load.css.files", ex);
 451 ////        }
 452 ////    }
 453 //
 454 //    private static Set<String> getStyleClasses(final URL url) throws Exception {
 455 //        Set<String> styleClasses = new HashSet<>();
 456 //        Stylesheet s;
 457 //        try {
 458 //            s = CSSParser.getInstance().parse(url);
 459 //        } catch (IOException ex) {
 460 //            Utils.println("Invalid Stylesheet " + url);
 461 //            return styleClasses;
 462 //        }
 463 //        if (s == null) {
 464 //            // The parsed CSS file was empty. No parsing occured.
 465 //            return styleClasses;
 466 //        }
 467 //        for (Rule r : s.getRules()) {
 468 //            for (Selector ss : r.getSelectors()) {
 469 //                if (ss instanceof SimpleSelector) {
 470 //                    SimpleSelector simple = (SimpleSelector) ss;
 471 //                    styleClasses.addAll(simple.getStyleClasses());
 472 //                } else {
 473 //                    if (ss instanceof CompoundSelector) {
 474 //                        CompoundSelector cs = (CompoundSelector) ss;
 475 //                        for (Selector selector : cs.getSelectors()) {
 476 //                            if (selector instanceof SimpleSelector) {
 477 //                                SimpleSelector simple = (SimpleSelector) selector;
 478 //                                styleClasses.addAll(simple.getStyleClasses());


1006 //    // Should be private, used by unit tests
1007 //    static void startListeningToCssErrors(Project project) {
1008 //        startListeningToCssErrors();
1009 //        Frame frame = Frame.get(project);
1010 //        if (frame == null) { // Loading the project
1011 //            LOADING_PROJECT = project;
1012 //        } else {
1013 //            CssError.setCurrentScene(frame.getScene());
1014 //        }
1015 //    }
1016 //    
1017 //    // Should be private, used by unit tests
1018 //    static void stopListeningToCssErrors(Project proj) {
1019 //        stopListeningToCssErrors();
1020 //        LOADING_PROJECT = null;
1021 //        CssError.setCurrentScene(null);
1022 //    }
1023 //
1024 //    private static final CssParsingListener cssListener = new CssParsingListener();
1025 //    static {
1026 //        StyleManager.errorsProperty().addListener(cssListener);
1027 //    }
1028 //    
1029 //    public static void updateStylesheets(Project project, Parent parent, File file) {
1030 //        startListeningToCssErrors();
1031 //        try {
1032 //            
1033 //            // This is done outside any transaction.
1034 //            // replace the file that has been updated. And only this file.
1035 //            // Replacing the whole list fires unwanted file parsing.
1036 //            for(int i = 0; i < parent.getStylesheets().size(); i++){
1037 //                String url = parent.getStylesheets().get(i);
1038 //                if(url.toLowerCase().startsWith("file:")){//NOI18N
1039 //                    File f;
1040 //                    try {
1041 //                        f = new File(new URL(url).toURI());
1042 //                    } catch (Exception ex) {
1043 //                        Utils.println(ex.toString(), ex);
1044 //                        continue;
1045 //                    }
1046 //                    if(file.equals(f)){


1134 //        return fullSet;
1135 //    }
1136 //    
1137 //    public static Set<String> retrieveAlternateStyleClasses(Class<?> type){
1138 //        Set<String> alternates = ALTERNATE_STYLECLASSES.get(type);
1139 //        Set<String> fullSet = new HashSet<>();
1140 //        if(alternates != null){
1141 //            fullSet.addAll(alternates);
1142 //        }
1143 //        return fullSet;
1144 //    }
1145 //
1146 //    public static boolean checkStyle(final Project project, String style) {
1147 //        if (style == null || style.equals("")) { //NOI18N
1148 //            return true;
1149 //        }
1150 //        Stylesheet s = null;
1151 //        // Synchronous listener to get errors synchronously.
1152 //        // Required for synchronous validation
1153 //        CssInlineStyleListener listener = new CssInlineStyleListener();
1154 //        StyleManager.errorsProperty().addListener(listener);
1155 //        startListeningToCssErrors(project);
1156 //        try {
1157 //            try {
1158 //                s = CSSParser.getInstance().parseInlineStyle(new StyleableStub(style));
1159 //            }catch(final RuntimeException ex){
1160 //                // Parser exception that has not been tracked by the listener.
1161 //                // Bug in CSS RT
1162 //                Utils.println("Unexpected error parsing CSS style", ex); //NOI18N
1163 //            }
1164 //        } finally {
1165 //            stopListeningToCssErrors(project);
1166 //            StyleManager.errorsProperty().removeListener(listener);
1167 //        }
1168 //        
1169 //        return s != null && listener.getErrors().isEmpty();
1170 //    }
1171 //    
1172 //    public static void addStyleSheet(Frame frame){
1173 //        FileChooserWrapper fileChooser = FileChooserWrapper.create().
1174 //                extensionFilters(SS_EXTENSIONS).
1175 //                title(Utils.getI18N().getString("window.title.add.style.sheet")).build();
1176 //        File f = fileChooser.showOpenDialog(frame.getStage());
1177 //        if(f!= null){
1178 //            Project p = frame.getProject();
1179 //            List<File> files = p.getStylesheets();
1180 //            if(!files.contains(f)){
1181 //                List<File> newLst = Utils.newList();
1182 //                newLst.addAll(files);
1183 //                newLst.add(f);
1184 //                p.setStylesheets(newLst);
1185 //            }
1186 //        }


1236 //        @Override
1237 //        public Styleable getStyleableParent() {
1238 //            return null;
1239 //        }
1240 //
1241 //        @Override
1242 //        @SuppressWarnings("rawtypes")
1243 //        public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() {
1244 //            return Collections.emptyList();
1245 //        }
1246 //
1247 //        @Override
1248 //        public ObservableSet<PseudoClass> getPseudoClassStates() {
1249 //            return null; // TODO Return something useful
1250 //        }
1251 //    }
1252 //    
1253 //    public static Set<String> lookupImagesInStyle(String style) {
1254 //        if (style != null) {
1255 //            try {
1256 //                Stylesheet s = CSSParser.getInstance().parseInlineStyle(new StyleableStub(style));
1257 //                return lookupImagesInStylesheet(s);
1258 //            } catch (RuntimeException ex) {
1259 //                Utils.println(ex.getMessage());
1260 //            }
1261 //        }
1262 //        return null;
1263 //    }
1264 //    
1265 //    public static Set<String> lookupImagesInCssFile(Project project, URI uri, boolean isRemoval) {
1266 //        if (uri != null) {
1267 //            if (!isRemoval) {
1268 //                startListeningToCssErrors(project);
1269 //            }
1270 //            try {
1271 //                Stylesheet s = CSSParser.getInstance().parse(uri.toURL());
1272 //                return lookupImagesInStylesheet(s);
1273 //            } catch (Exception ex) {
1274 //                Utils.println(ex.getMessage());
1275 //            } finally {
1276 //                if (!isRemoval) {
1277 //                    stopListeningToCssErrors(project);
1278 //                }
1279 //            }
1280 //        }
1281 //        return null;
1282 //    }
1283 //    
1284 //    private static Set<String> lookupImagesInStylesheet(Stylesheet s) {
1285 //        Set<String> files = Utils.newSet();
1286 //        try {
1287 //            for (Rule r : s.getRules()) {
1288 //                for (Declaration d : r.getDeclarations()) {
1289 //                    if (IMG_PROPERTIES.contains(d.getProperty())) {
1290 //                        Object obj = d.getParsedValue().convert(null);
1291 //                        if (obj instanceof String) {




 366 //    public static final String HIERARCHY_CSS = Utils.getResourceURL(Frame.class, "css_stylesheets/Hierarchy.css"); //NOI18N
 367 //    public static final String INSPECTOR_CSS = Utils.getResourceURL(Frame.class, "css_stylesheets/Inspector.css"); //NOI18N
 368 //    public static final String CSS_VIEWER_CSS = Utils.getResourceURL(Frame.class, "css_stylesheets/CssViewer.css"); //NOI18N
 369 //
 370 //    public static final String POPUP_CSS = Utils.getResourceURL(Frame.class, "css_stylesheets/Popup.css"); //NOI18N
 371 //    
 372 //    public static final String SCENE_BUILDER_THEME_STYLECLASS = "SCENE_BUILDER_THEME"; //NOI18N
 373 //    private static final String SCENE_BUILDER_WIN_FONT_STYLECLASS = "SCENE_BUILDER_WIN_FONT"; //NOI18N
 374 //    private static final String SCENE_BUILDER_WINXP_FONT_STYLECLASS = "SCENE_BUILDER_WINXP_FONT"; //NOI18N
 375 //    
 376 //    public static final String CONTENT_AREA_ID = "JFX_SB_ContentArea"; //NOI18N
 377 //    private static final String ROOT_STYLECLASS = "root"; //NOI18N
 378 //    private static Stylesheet STYLE_SHEET_TOOL_CSS = null;
 379 //    private static final Map<Class<?>, Set<String>> ALTERNATE_STYLECLASSES = new HashMap<>();
 380 //
 381 //    static Set<FileChooser.ExtensionFilter> SS_EXTENSIONS =
 382 //            Collections.singleton(new FileChooser.ExtensionFilter(Utils.getI18N().getString("popup.style.sheets"), "*.css", "*.bss")); //NOI18N
 383 //    
 384 //    static {
 385 //        try {
 386 //            STYLE_SHEET_TOOL_CSS = new CssParser().parse(new URL(TOOL_ROOT_CSS));
 387 //        } catch (IOException ex) {
 388 //            Utils.println("Failed to parse " + TOOL_ROOT_CSS, ex); //NOI18N
 389 //        }
 390 //        Set<String> alternates = new HashSet<>();
 391 //        alternates.add("floating"); //NOI18N
 392 //        ALTERNATE_STYLECLASSES.put(TabPane.class, alternates);
 393 //    }
 394 //    //Properties that are impacting CSS/Pages.
 395 //    private static final Set<Prop> CSS_IMPACT = 
 396 //            SetBuilder.<Prop>make().
 397 //            add(PropUtils.Parent_STYLESHEETS).
 398 //            add(PropUtils.Node_ID).add(PropUtils.Styleable_STYLE_CLASS).add(PropUtils.Node_STYLE).
 399 //            add(PropUtils.Tab_ID).add(PropUtils.Tab_STYLE).
 400 //            add(PropUtils.PopupControl_ID).add(PropUtils.PopupControl_STYLE).
 401 //            add(PropUtils.Menu_ITEMS).buildUnmodifiable();
 402 //    private static final WeakIdentityHashMap<Parent, List<StyleClasses>> STYLECLASSES_SETS = WeakIdentityHashMap.make();
 403 //    
 404 //    public static void trackNode(final Node node) {
 405 //        attachMapToNode(node);
 406 //    }


 438 ////    public static void loadAuthorCssFiles(Frame frame) {
 439 ////        Parent contentParent = getParent(frame);
 440 ////        final Project project = frame.getProject();
 441 ////        try {
 442 ////            clearAuthorStyleSheets(contentParent);
 443 ////            for (Css css : project.getScreenData().getAuthorCss()) {
 444 ////                if (css.isActive()) {
 445 ////                    URL u = css.getFile().toURI().toURL();
 446 ////                    loadAuthorCss(frame, contentParent, u);
 447 ////                }
 448 ////            }
 449 ////        } catch (Exception ex) {
 450 ////            frame.printWarning("messagebar.cannot.load.css.files", ex);
 451 ////        }
 452 ////    }
 453 //
 454 //    private static Set<String> getStyleClasses(final URL url) throws Exception {
 455 //        Set<String> styleClasses = new HashSet<>();
 456 //        Stylesheet s;
 457 //        try {
 458 //            s = new CssParser().parse(url);
 459 //        } catch (IOException ex) {
 460 //            Utils.println("Invalid Stylesheet " + url);
 461 //            return styleClasses;
 462 //        }
 463 //        if (s == null) {
 464 //            // The parsed CSS file was empty. No parsing occured.
 465 //            return styleClasses;
 466 //        }
 467 //        for (Rule r : s.getRules()) {
 468 //            for (Selector ss : r.getSelectors()) {
 469 //                if (ss instanceof SimpleSelector) {
 470 //                    SimpleSelector simple = (SimpleSelector) ss;
 471 //                    styleClasses.addAll(simple.getStyleClasses());
 472 //                } else {
 473 //                    if (ss instanceof CompoundSelector) {
 474 //                        CompoundSelector cs = (CompoundSelector) ss;
 475 //                        for (Selector selector : cs.getSelectors()) {
 476 //                            if (selector instanceof SimpleSelector) {
 477 //                                SimpleSelector simple = (SimpleSelector) selector;
 478 //                                styleClasses.addAll(simple.getStyleClasses());


1006 //    // Should be private, used by unit tests
1007 //    static void startListeningToCssErrors(Project project) {
1008 //        startListeningToCssErrors();
1009 //        Frame frame = Frame.get(project);
1010 //        if (frame == null) { // Loading the project
1011 //            LOADING_PROJECT = project;
1012 //        } else {
1013 //            CssError.setCurrentScene(frame.getScene());
1014 //        }
1015 //    }
1016 //    
1017 //    // Should be private, used by unit tests
1018 //    static void stopListeningToCssErrors(Project proj) {
1019 //        stopListeningToCssErrors();
1020 //        LOADING_PROJECT = null;
1021 //        CssError.setCurrentScene(null);
1022 //    }
1023 //
1024 //    private static final CssParsingListener cssListener = new CssParsingListener();
1025 //    static {
1026 //        CssParser.errorsProperty().addListener(cssListener);
1027 //    }
1028 //    
1029 //    public static void updateStylesheets(Project project, Parent parent, File file) {
1030 //        startListeningToCssErrors();
1031 //        try {
1032 //            
1033 //            // This is done outside any transaction.
1034 //            // replace the file that has been updated. And only this file.
1035 //            // Replacing the whole list fires unwanted file parsing.
1036 //            for(int i = 0; i < parent.getStylesheets().size(); i++){
1037 //                String url = parent.getStylesheets().get(i);
1038 //                if(url.toLowerCase().startsWith("file:")){//NOI18N
1039 //                    File f;
1040 //                    try {
1041 //                        f = new File(new URL(url).toURI());
1042 //                    } catch (Exception ex) {
1043 //                        Utils.println(ex.toString(), ex);
1044 //                        continue;
1045 //                    }
1046 //                    if(file.equals(f)){


1134 //        return fullSet;
1135 //    }
1136 //    
1137 //    public static Set<String> retrieveAlternateStyleClasses(Class<?> type){
1138 //        Set<String> alternates = ALTERNATE_STYLECLASSES.get(type);
1139 //        Set<String> fullSet = new HashSet<>();
1140 //        if(alternates != null){
1141 //            fullSet.addAll(alternates);
1142 //        }
1143 //        return fullSet;
1144 //    }
1145 //
1146 //    public static boolean checkStyle(final Project project, String style) {
1147 //        if (style == null || style.equals("")) { //NOI18N
1148 //            return true;
1149 //        }
1150 //        Stylesheet s = null;
1151 //        // Synchronous listener to get errors synchronously.
1152 //        // Required for synchronous validation
1153 //        CssInlineStyleListener listener = new CssInlineStyleListener();
1154 //        CssParser.errorsProperty().addListener(listener);
1155 //        startListeningToCssErrors(project);
1156 //        try {
1157 //            try {
1158 //                s = new CssParser().parseInlineStyle(new StyleableStub(style));
1159 //            }catch(final RuntimeException ex){
1160 //                // Parser exception that has not been tracked by the listener.
1161 //                // Bug in CSS RT
1162 //                Utils.println("Unexpected error parsing CSS style", ex); //NOI18N
1163 //            }
1164 //        } finally {
1165 //            stopListeningToCssErrors(project);
1166 //            CssParser.errorsProperty().removeListener(listener);
1167 //        }
1168 //        
1169 //        return s != null && listener.getErrors().isEmpty();
1170 //    }
1171 //    
1172 //    public static void addStyleSheet(Frame frame){
1173 //        FileChooserWrapper fileChooser = FileChooserWrapper.create().
1174 //                extensionFilters(SS_EXTENSIONS).
1175 //                title(Utils.getI18N().getString("window.title.add.style.sheet")).build();
1176 //        File f = fileChooser.showOpenDialog(frame.getStage());
1177 //        if(f!= null){
1178 //            Project p = frame.getProject();
1179 //            List<File> files = p.getStylesheets();
1180 //            if(!files.contains(f)){
1181 //                List<File> newLst = Utils.newList();
1182 //                newLst.addAll(files);
1183 //                newLst.add(f);
1184 //                p.setStylesheets(newLst);
1185 //            }
1186 //        }


1236 //        @Override
1237 //        public Styleable getStyleableParent() {
1238 //            return null;
1239 //        }
1240 //
1241 //        @Override
1242 //        @SuppressWarnings("rawtypes")
1243 //        public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() {
1244 //            return Collections.emptyList();
1245 //        }
1246 //
1247 //        @Override
1248 //        public ObservableSet<PseudoClass> getPseudoClassStates() {
1249 //            return null; // TODO Return something useful
1250 //        }
1251 //    }
1252 //    
1253 //    public static Set<String> lookupImagesInStyle(String style) {
1254 //        if (style != null) {
1255 //            try {
1256 //                Stylesheet s = new CssParser().parseInlineStyle(new StyleableStub(style));
1257 //                return lookupImagesInStylesheet(s);
1258 //            } catch (RuntimeException ex) {
1259 //                Utils.println(ex.getMessage());
1260 //            }
1261 //        }
1262 //        return null;
1263 //    }
1264 //    
1265 //    public static Set<String> lookupImagesInCssFile(Project project, URI uri, boolean isRemoval) {
1266 //        if (uri != null) {
1267 //            if (!isRemoval) {
1268 //                startListeningToCssErrors(project);
1269 //            }
1270 //            try {
1271 //                Stylesheet s = new CssParser().parse(uri.toURL());
1272 //                return lookupImagesInStylesheet(s);
1273 //            } catch (Exception ex) {
1274 //                Utils.println(ex.getMessage());
1275 //            } finally {
1276 //                if (!isRemoval) {
1277 //                    stopListeningToCssErrors(project);
1278 //                }
1279 //            }
1280 //        }
1281 //        return null;
1282 //    }
1283 //    
1284 //    private static Set<String> lookupImagesInStylesheet(Stylesheet s) {
1285 //        Set<String> files = Utils.newSet();
1286 //        try {
1287 //            for (Rule r : s.getRules()) {
1288 //                for (Declaration d : r.getDeclarations()) {
1289 //                    if (IMG_PROPERTIES.contains(d.getProperty())) {
1290 //                        Object obj = d.getParsedValue().convert(null);
1291 //                        if (obj instanceof String) {