📄 toolbarproperties.java
字号:
/* * ToolBarProperties.java * * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */package org.underworldlabs.swing.toolbar;import java.io.CharArrayWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Vector;import javax.swing.SwingUtilities;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.sax.SAXSource;import javax.xml.transform.stream.StreamResult;import org.xml.sax.Attributes;import org.xml.sax.ContentHandler;import org.xml.sax.DTDHandler;import org.xml.sax.EntityResolver;import org.xml.sax.ErrorHandler;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.AttributesImpl;import org.xml.sax.helpers.DefaultHandler;import org.underworldlabs.swing.GUIUtils;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the * release of version 3.0.0beta1 has meant a * resetting of CVS revision numbers. * ---------------------------------------------------------- *//** * * @author Takis Diakoumis * @version $Revision: 1.6 $ * @date $Date: 2006/06/28 08:34:57 $ */public class ToolBarProperties { /** The user defined tools */ private static Vector tools; /** The default tools */ private static Vector defaultTools; /** the tools XML conf file path */ private static String toolsConfPath; /** the default tools XML conf resource file path */ private static String defaultToolsConfPath; // ----------------------------------- // --- XML elements and attributes --- // ----------------------------------- private static final String EQ_TOOLBARS = "system-toolbars"; private static final String TOOLBAR = "toolbar"; private static final String ROW = "row"; private static final String POSITION = "position"; private static final String LOC_X = "loc-x"; private static final String RESIZE_OFFSET_X = "resize-offset-x"; private static final String MINIMUM_WIDTH = "minimum-width"; private static final String PREFERRED_WIDTH = "preferred-width"; private static final String CURRENT_WIDTH = "current-width"; private static final String BUTTONS = "buttons"; private static final String CONSTRAINTS = "constraints"; private static final String BUTTON = "button"; private static final String NAME = "name"; private static final String ACTION_ID = "action-id"; private static final String ID = "id"; private static final String VISIBLE = "visible"; private static final String ORDER = "order"; // ----------------------------------- private static final String EMPTY = ""; /** Whether the tools have been loaded and config files passed. */ private static boolean toolsLoaded; public static void init(String _toolsConfPath, String _defaultToolsConfPath) { toolsConfPath = _toolsConfPath; defaultToolsConfPath = _defaultToolsConfPath; // TODO: do we allow null conf files with defaults only??? if (toolsConfPath != null) { loadTools(); } toolsLoaded = true; } private static void checkInit() { if (toolsConfPath == null) { throw new RuntimeException( "Tool configuration XML file is NULL or failed to load. " + "Ensure the init() method is run prior to retrieving " + "any tool conf information"); } } private static void checkDefaultInit() { if (defaultToolsConfPath == null) { throw new RuntimeException( "Default Tool configuration XML file resource is NULL " + "Ensure the init(...) method is called prior to retrieving " + "any tool conf information"); } } public static Vector getToolbarButtonsVector() { checkInit(); if (tools == null || tools.size() == 0) { loadTools(); } return tools; } public static ToolBarWrapper[] getToolbarButtonsArray() { checkInit(); if (tools == null || tools.size() == 0) { loadTools(); } return (ToolBarWrapper[])tools.toArray(new ToolBarWrapper[]{}); } public static Vector getDefaultToolbarButtonsVector() { checkDefaultInit(); if (defaultTools == null || defaultTools.size() == 0) { loadDefaults(false); } return defaultTools; } public static ToolBarWrapper[] getDefaultToolbarButtonsArray() { checkDefaultInit(); if (defaultTools == null || defaultTools.size() == 0) { loadDefaults(false); } return (ToolBarWrapper[])defaultTools.toArray(new ToolBarWrapper[]{}); } public static void setToolBarConstraints(String name, ToolBarConstraints tbc) { ToolBarWrapper toolBar = getToolBar(name); toolBar.setConstraints(tbc); } public static void removeToolBar(String name) { tools.remove(getToolBar(name)); } public static void resetToolBar(String name, ToolBarWrapper toolBar) { tools.remove(getToolBar(name)); tools.add(toolBar); } public static void setToolBarVisible(String name, boolean visible) { ToolBarWrapper toolBar = getToolBar(name); toolBar.setVisible(visible); } public static boolean isToolBarVisible(String name) { ToolBarWrapper toolBar = getToolBar(name); return toolBar.isVisible(); } public static ToolBarWrapper getDefaultToolBar(String name) { checkDefaultInit(); if (defaultTools == null || defaultTools.isEmpty()) { loadDefaults(false); } ToolBarWrapper toolBar = null; for (int i = 0, k = defaultTools.size(); i < k; i++) { toolBar = (ToolBarWrapper)defaultTools.elementAt(i); if (name.compareTo(toolBar.getName()) == 0) { break; } } return toolBar; } public static int getNextToolbarRow() { int row; int currentMaxRow = -1; ToolBarWrapper[] toolBars = getToolbarButtonsArray(); for (int i = 0; i < toolBars.length; i++) { row = toolBars[i].getConstraints().getRow(); if (row > currentMaxRow) { currentMaxRow = row; } } return currentMaxRow + 1; } public static ToolBarWrapper getToolBar(String name) { if (tools == null || tools.size() == 0) { loadTools(); } ToolBarWrapper toolBar = null; for (int i = 0, k = tools.size(); i < k; i++) { toolBar = (ToolBarWrapper)tools.elementAt(i); if (name.compareTo(toolBar.getName()) == 0) { break; } } return toolBar; } public static int saveTools() { OutputStream os = null; try { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); ToolsParser cp = new ToolsParser(); File file = new File(toolsConfPath); os = new FileOutputStream(file); SAXSource source = new SAXSource(cp, new ToolbarButtonsSource()); StreamResult r = new StreamResult(os); transformer.transform(source, r); return 1; } catch (Exception e) { e.printStackTrace(); return 0; } finally { if (os != null) { try { os.close(); } catch (IOException e) {} } } } public static void reloadTools(boolean loadDefaults) { if (loadDefaults) { defaultTools = null; loadDefaults(false); } else { loadTools(); } } private static synchronized void loadDefaults(boolean setDefaults) { if (defaultTools != null && defaultTools.size() > 0) { return; } InputStream input = null; ClassLoader cl = ToolBarProperties.class.getClassLoader(); if (cl != null) { input = cl.getResourceAsStream(defaultToolsConfPath); } else { input = ClassLoader.getSystemResourceAsStream(defaultToolsConfPath); } try { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); XMLToolHandler handler = new XMLToolHandler(); parser.parse(input, handler); defaultTools = handler.getToolsVector(); if (setDefaults) { int size = defaultTools.size(); tools = new Vector(size); ToolBarWrapper toolBar = null; for (int i = 0; i < size; i++) { toolBar = (ToolBarWrapper)defaultTools.elementAt(i); tools.add(toolBar.clone()); } } } catch (Exception e) { e.printStackTrace(); GUIUtils.displayErrorMessage( null, "Error opening default tools definitions."); } finally { if (input != null) { try { input.close(); } catch (IOException e) {} } } } // checks for new tools added from the defaults private static void compareTools() { boolean hasButton = false; boolean rebuild = false; ToolBarWrapper[] defaultsArray = getDefaultToolbarButtonsArray(); ToolBarWrapper[] toolsArray = getToolbarButtonsArray(); ToolBarWrapper currentToolBar = null; for (int i = 0; i < defaultsArray.length; i++) { String name = defaultsArray[i].getName(); ToolBarButton[] buttons = defaultsArray[i].getButtonsArray(); if (buttons == null) { continue; } for (int j = 0; j < toolsArray.length; j++) { if (toolsArray[j].getName().compareTo(name) == 0) { currentToolBar = toolsArray[j]; break; } } ToolBarButton[] _buttons = currentToolBar.getButtonsArray(); if (_buttons == null) { continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -