📄 userlayoutproperties.java
字号:
} } /** * Sets the layout definition objects. * * @param the layout objects */ public void setLayoutObjects(Map<String, UserLayoutObject> layoutObjects) { this.layoutObjects = layoutObjects; } /** * Loads the layout definitions from file. * If the user file does not exist this will create it for the first time. */ private synchronized void load() { //Log.info("Loading layout options."); String confPath = SystemUtilities.getUserPropertiesPath(); String from = "org/executequery/layout-preferences.xml"; String to = confPath + "layout-preferences.xml"; File file = new File(to); if (layoutObjects == null) { layoutObjects = new HashMap<String,UserLayoutObject>(); } if (file.exists()) { InputStream in = null; try { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); XMLLayoutHandler handler = new XMLLayoutHandler(); Log.debug("Loading layout preferences from: " + file.getAbsolutePath()); in = new FileInputStream(file); parser.parse(in, handler); } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) {} } } // set the system property values for (Iterator i = layoutObjects.keySet().iterator(); i.hasNext();) { String key = i.next().toString(); UserLayoutObject object = layoutObjects.get(key); SystemProperties.setBooleanProperty("user", key, object.isVisible()); } } else { // create the file for the first time try { FileUtils.copyResource(from, to); } catch (IOException ioExc) {} // reload the new file load(); } } /** * Saves the layout definitions to file. */ private synchronized void save() { if (layoutObjects == null) { return; } OutputStream os = null; try { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); LayoutParser parser = new LayoutParser(); File file = new File(SystemUtilities.getUserPropertiesPath() + "layout-preferences.xml"); Log.debug("Saving layout preferences to: " + file.getAbsolutePath()); os = new FileOutputStream(file); SAXSource source = new SAXSource(parser, new LayoutInputSource()); StreamResult r = new StreamResult(os); transformer.transform(source, r); } catch (Exception e) { Log.debug("Error saving layout-preferences.xml", e); GUIUtilities.displayExceptionErrorDialog( "Error storing user layout change:\n" + e.getMessage(), e); } finally { try { if (os != null) { os.close(); } } catch (IOException e) {} } /* XMLEncoder encoder = null; BufferedOutputStream outputStream = null; try { String path = SystemUtilities.getUserPropertiesPath() + "layout-preferences.xml"; outputStream = new BufferedOutputStream(new FileOutputStream(path)); encoder = new XMLEncoder(outputStream); encoder.writeObject(layoutObjects); } catch (IOException e) { e.printStackTrace(); } finally { try { if (encoder != null) { encoder.close(); } if (outputStream != null) { outputStream.close(); } } catch (IOException e) {} } */ } // ---------------------------------- // XML parsing // ---------------------------------- static class XMLLayoutHandler extends DefaultHandler { private UserLayoutObject object = new UserLayoutObject(); private CharArrayWriter contents = new CharArrayWriter(); public XMLLayoutHandler() {} public void startElement(String nameSpaceURI, String localName, String qName, Attributes attrs) { contents.reset(); } public void endElement(String nameSpaceURI, String localName, String qName) { if (object == null) { object = new UserLayoutObject(); } if (localName.equals("key")) { object.setKey(contents.toString()); } else if (localName.equals("index")) { object.setIndex(Integer.parseInt(contents.toString())); } else if (localName.equals("position")) { object.setPosition(Integer.parseInt(contents.toString())); } else if (localName.equals("visible")) { object.setVisible(new Boolean(contents.toString()).booleanValue()); } else if (localName.equals("minimised")) { object.setMinimised(new Boolean(contents.toString()).booleanValue()); } else if (localName.equals("docked-tab")) { layoutObjects.put(object.getKey(), object); object = null; } } public void characters(char[] data, int start, int length) { contents.write(data, start, length); } public void ignorableWhitespace(char[] data, int start, int length) { characters(data, start, length); } public void error(SAXParseException spe) throws SAXException { throw new SAXException(spe.getMessage()); } } // XMLHandler static class LayoutParser implements XMLReader { private String nsu = ""; private Attributes atts = new AttributesImpl(); private static String rootElement = "docked-tab-components"; private static String DOCKED_TAB = "docked-tab"; private static String KEY = "key"; private static String INDEX = "index"; private static String POSITION = "position"; private static String VISIBLE = "visible"; private static String MINIMISED = "minimised"; private ContentHandler handler; private static char[] newLine = {'\n'}; private static String indent_1 = "\n "; private static String indent_2 = "\n "; public LayoutParser() {} public void parse(InputSource input) throws SAXException, IOException { if (!(input instanceof LayoutInputSource)) throw new SAXException("Parser can only accept a LayoutInputSource"); parse((LayoutInputSource)input); } public void parse(LayoutInputSource input) throws IOException, SAXException { try { if (handler == null) { throw new SAXException("No content handler"); } Map<String,UserLayoutObject> objects = input.getLayoutObjects(); handler.startDocument(); handler.ignorableWhitespace(newLine, 0, 1); handler.startElement(nsu, rootElement, rootElement, atts); handler.ignorableWhitespace(newLine, 0, 1); String marker = null; for (Iterator i = objects.keySet().iterator(); i.hasNext();) { handler.ignorableWhitespace(indent_1.toCharArray(), 0, indent_1.length()); handler.startElement(nsu, DOCKED_TAB, DOCKED_TAB, atts); String key = i.next().toString(); UserLayoutObject object = objects.get(key); writeXML(KEY, object.getKey(), indent_2); writeXML(INDEX, Integer.toString(object.getIndex()), indent_2); writeXML(POSITION, Integer.toString(object.getPosition()), indent_2); writeXML(VISIBLE, Boolean.toString(object.isVisible()), indent_2); writeXML(MINIMISED, Boolean.toString(object.isMinimised()), indent_2); handler.ignorableWhitespace(indent_1.toCharArray(), 0, indent_1.length()); handler.endElement(nsu, DOCKED_TAB, DOCKED_TAB); handler.ignorableWhitespace(newLine, 0, 1); } handler.ignorableWhitespace(newLine, 0, 1); handler.endElement(nsu, rootElement, rootElement); handler.endDocument(); } catch (Exception e) { e.printStackTrace(); } } private void writeXML(String name, String line, String space) throws SAXException { if (line == null) { line = Constants.EMPTY; } int textLength = line.length(); handler.ignorableWhitespace(space.toCharArray(), 0, space.length()); handler.startElement(nsu, name, name, atts); handler.characters(line.toCharArray(), 0, textLength); handler.endElement(nsu, name, name); } public void setContentHandler(ContentHandler handler) { this.handler = handler; } public ContentHandler getContentHandler() { return this.handler; } public void setErrorHandler(ErrorHandler handler) {} public ErrorHandler getErrorHandler() { return null; } public void parse(String systemId) throws IOException, SAXException { } public DTDHandler getDTDHandler() { return null; } public EntityResolver getEntityResolver() { return null; } public void setEntityResolver(EntityResolver resolver) {} public void setDTDHandler(DTDHandler handler) {} public Object getProperty(String name) { return null; } public void setProperty(String name, java.lang.Object value) {} public void setFeature(String name, boolean value) {} public boolean getFeature(String name) { return false; } } static class LayoutInputSource extends InputSource { public LayoutInputSource() {} public Map<String,UserLayoutObject> getLayoutObjects() { return layoutObjects; } } // class LayoutInputSource static class LayoutSorter implements Comparator { public int compare(Object obj1, Object obj2) { UserLayoutObject value1 = (UserLayoutObject)obj1; UserLayoutObject value2 = (UserLayoutObject)obj2; int index1= value1.getIndex(); int index2= value2.getIndex(); if (index1 < index2) { return -1; } else if (index1 > index2) { return 1; } else { return 0; } } } // class LayoutSorter }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -