public List importAndApplyStyleMapFile()

in CasViewerEclipsePlugin/uimaj-ep-casviewer-core/src/main/java/org/apache/uima/casviewer/ui/internal/style/TypeSystemStyle.java [526:664]


    public List<TypeStyle> importAndApplyStyleMapFile(List<String> typeNames, final File styleMapFile) 
    {
        // Trace.err("Import: " + styleMapFile.getAbsolutePath());
        if (!styleMapFile.exists()) {
            return null;
        }
        setImportedStyleMapFile(styleMapFile.getAbsolutePath());
        
        // Remove all previous mapping
        hasTypeSystemStyle = false;
//        indexUsedColors = 0;
        preSelectedList.clear();
//        colorList.clear();
        List<String> hiddenList = new ArrayList<String>();
        
        Map<String, TypeStyle> typeNameToTypeStyleMap = new HashMap<String, TypeStyle>();
        
        //  hiddenList.add("uima.cpm.FileLocation"); //$NON-NLS

        Document parse = null;
        try {
            DocumentBuilder db = 
                DocumentBuilderFactory.newInstance().newDocumentBuilder();
            FileInputStream stream = new FileInputStream(styleMapFile);
            parse = db.parse(stream);
            stream.close(); 

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
            return null;

        } catch (FactoryConfigurationError e) {
            e.printStackTrace();
            return null;

        } catch (SAXException e) {
            e.printStackTrace();
            return null;

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        final Node root = parse.getDocumentElement();
        final NodeList nodeList = root.getChildNodes();
        // final ColorParser cParser = new ColorParser();

        for (int i = 0; i < nodeList.getLength(); ++i) {
            final Node node = nodeList.item(i);
            final String nodeName = node.getNodeName();
            // "rule" node ?
            if ( ! nodeName.equals("rule") ) { //$NON-NLS-1$
                continue;
            }
            
            // Collect type or pattern, label, and color text style
            NodeList childrenList = node.getChildNodes();
            String type = ""; //$NON-NLS-1$ 
            String label = ""; //$NON-NLS-1$ 
            String colorText = ""; //$NON-NLS-1$ 
            for (int j = 0; j < childrenList.getLength(); ++j) {
                final Node child = childrenList.item(j);
                final String childName = child.getNodeName();

                if (childName.equals("pattern")) { //$NON-NLS-1$ 
                    type = getTextValue(child);
                } else if (childName.equals("label")) { //$NON-NLS-1$ 
                    label = getTextValue(child);
                } else if (childName.equals("style")) { //$NON-NLS-1$ 
                    colorText = getTextValue(child);
                }
            }
            
            // Create a new TypeStyle
            TypeStyle typeStyle = createTypeStyle (type, label, colorText);
            if (typeStyle.isChecked()) {
                preSelectedList.add(typeStyle.getTypeName()); 
            }
            if (!typeStyle.isHidden()) {
//                colorList.add(typeStyle.getBackground()); 
            } else {
                // Hidden Type
                hiddenList.add(typeStyle.getTypeName()); 
            }
            
            typeNameToTypeStyleMap.put(typeStyle.getTypeName(), typeStyle);
        } // for
        hasTypeSystemStyle = true;
        
        // Apply the imported style file to types
        defaultTypeStyleMap.clear();
        if (typeNames != null) {
            // Save type names
            typeNameListSortedByDefColors.clear();
            typeNameListSortedByDefColors = new ArrayList<String>(typeNames);
        }
        List<TypeStyle> returnedList = new ArrayList<TypeStyle> (typeNames.size());
        for (String name: typeNameListSortedByDefColors) {
            TypeStyle style = typeNameToTypeStyleMap.get(name);
            if (style == null) {
                // Assign empty style
                style = new TypeStyle(name, TypeStyle.getShortName(name), null, null);
            }
            returnedList.add(style);
            defaultTypeStyleMap.put(name, style);
        }
        
        // Set hidden type names
        setHiddenTypeNames(hiddenList);
        
        return returnedList;
//        Iterator iter = defaultTypeStyleMap.entrySet().iterator();
//        int i = 0;
//        while (iter.hasNext()) {
//            Map.Entry e = (Map.Entry) iter.next();
//            if (typeNameToTypeStyleMap.containsKey(e.getKey())) {
//                // Overlap, i.e. type in default style is also in imported style
//                ((TypeStyle) e.getValue()).copyStyle((TypeStyle)typeNameToTypeStyleMap.get(e.getKey()));
//            } else {
//                // Clear style in defaultTypeStyleMap
//                ((TypeStyle) e.getValue()).clearStyle ();
//            }
//        }
        // mergeColors(styledTypeMap, mapBgFgColors);

//      _viewer.assignColorsFromList(colorList, typeList);
//      _viewer.assignCheckedFromList(notCheckedList);

//      hiddenList.add(HIDDEN_TYPES[0]); 
//      final String[] hiddenArr = new String[hiddenList.size()];
//      hiddenList.toArray(hiddenArr);
//      _viewer.setHiddenTypes(hiddenArr);

    }