001: /*
002: * $Id: JGraphpadStatusBar.java,v 1.2 2005/08/05 19:56:23 gaudenz Exp $
003: * Copyright (c) 2001-2005, Gaudenz Alder
004: *
005: * All rights reserved.
006: *
007: * See LICENSE file for license details. If you are unable to locate
008: * this file please contact info (at) jgraph (dot) com.
009: */
010: package com.jgraph.pad.factory;
011:
012: import java.awt.Component;
013: import java.awt.GridBagConstraints;
014: import java.awt.GridBagLayout;
015: import java.awt.event.MouseEvent;
016: import java.awt.event.MouseMotionListener;
017: import java.beans.PropertyChangeEvent;
018: import java.beans.PropertyChangeListener;
019: import java.text.NumberFormat;
020:
021: import javax.swing.BorderFactory;
022: import javax.swing.JLabel;
023: import javax.swing.JPanel;
024: import javax.swing.event.TreeModelEvent;
025:
026: import org.jgraph.JGraph;
027: import org.w3c.dom.Node;
028:
029: import com.jgraph.JGraphEditor;
030: import com.jgraph.editor.JGraphEditorFile;
031: import com.jgraph.editor.JGraphEditorModel;
032: import com.jgraph.editor.JGraphEditorResources;
033: import com.jgraph.editor.factory.JGraphEditorDiagramPane;
034: import com.jgraph.editor.factory.JGraphEditorFactoryMethod;
035: import com.jgraph.pad.util.JGraphpadFocusManager;
036: import com.jgraph.pad.util.JGraphpadTreeModelAdapter;
037:
038: /**
039: * Status bar to display general information about the focused graph.
040: */
041: public class JGraphpadStatusBar extends JPanel {
042:
043: /**
044: * Holds the shared number formatter.
045: *
046: * @see NumberFormat#getInstance()
047: */
048: public static final NumberFormat numberFormat = NumberFormat
049: .getInstance();
050:
051: /**
052: * Holds the labels contained in the status bar.
053: */
054: protected JLabel infoLabel = new JLabel(getString("NoDocument")),
055: modifiedLabel = new JLabel(" "),
056: editableLabel = new JLabel(" "),
057: zoomLabel = new JLabel(" "), mouseLabel = new JLabel(" "),
058: spareLabel = new JLabel(" ");
059:
060: /**
061: * Constructs a new repository panel for the specified editor.
062: */
063: public JGraphpadStatusBar(JGraphEditor editor) {
064: setBorder(BorderFactory.createEmptyBorder(4, 5, 5, 4));
065: setLayout(new GridBagLayout());
066: numberFormat.setMaximumFractionDigits(0);
067:
068: GridBagConstraints c = new GridBagConstraints();
069: c.anchor = GridBagConstraints.NORTHWEST;
070: c.fill = GridBagConstraints.HORIZONTAL;
071: c.ipadx = 4;
072: c.ipady = 2;
073: c.weighty = 0.0;
074: c.weightx = 1.0;
075: c.gridwidth = 1;
076: c.gridx = 0;
077: c.gridy = 0;
078: c.weightx = 4;
079: add(infoLabel, c);
080:
081: c.gridx = 1;
082: c.weightx = 1;
083: add(modifiedLabel, c);
084:
085: c.gridx = 2;
086: c.weightx = 1;
087: add(editableLabel, c);
088:
089: c.gridx = 3;
090: c.weightx = 1;
091: add(zoomLabel, c);
092:
093: c.gridx = 4;
094: c.weightx = 1;
095: add(mouseLabel, c);
096:
097: c.gridx = 5;
098: c.weightx = 3;
099: add(spareLabel, c);
100:
101: // Installs a listener to update the mouse location
102: JGraphpadFocusManager.getCurrentGraphFocusManager()
103: .addMouseMotionListener(new MouseMotionListener() {
104:
105: /*
106: * (non-Javadoc)
107: */
108: public void mouseDragged(MouseEvent e) {
109: mouseLabel.setText(e.getX() + " : " + e.getY());
110: }
111:
112: /*
113: * (non-Javadoc)
114: */
115: public void mouseMoved(MouseEvent e) {
116: mouseLabel.setText(e.getX() + " : " + e.getY());
117: }
118: });
119:
120: // Installs a listener to listen to the focused graph and react
121: // to all kinds of changes, such as selection, model, cache etc.
122: JGraphpadFocusManager.getCurrentGraphFocusManager()
123: .addPropertyChangeListener(
124: new PropertyChangeListener() {
125:
126: /*
127: * (non-Javadoc)
128: */
129: public void propertyChange(
130: PropertyChangeEvent e) {
131: updateLabels();
132: }
133: });
134:
135: // Installs a listener to update the file state
136: editor.getModel().addTreeModelListener(
137: new JGraphpadTreeModelAdapter() {
138:
139: /*
140: * (non-Javadoc)
141: */
142: public void treeNodesInserted(TreeModelEvent e) {
143: treeNodesChanged(e);
144: }
145:
146: /*
147: * (non-Javadoc)
148: */
149: public void treeNodesRemoved(TreeModelEvent e) {
150: treeNodesChanged(e);
151: }
152:
153: /*
154: * (non-Javadoc)
155: */
156: public void treeNodesChanged(TreeModelEvent e) {
157: updateLabels();
158: }
159: });
160: }
161:
162: /**
163: * Shortcut method to {@link JGraphEditorResources#getString(String)}.
164: *
165: * @param key
166: * The key to return the resource string for.
167: */
168: public static String getString(String key) {
169: return JGraphEditorResources.getString(key);
170: }
171:
172: /**
173: * Invoked by the various listeners to update the labels. This
174: * implementation does not update the mouse label as it is updated by other
175: * means.
176: */
177: protected void updateLabels() {
178: JGraphpadFocusManager focusedGraph = JGraphpadFocusManager
179: .getCurrentGraphFocusManager();
180:
181: // Fetches the focused graph by first trying the permanent focus
182: // owner and if that is null, the last focused graph.
183: JGraph graph = focusedGraph.getFocusedGraph();
184: if (graph != null) {
185:
186: // Updates the various graph-related labels
187: // Shows selection or graph details in the info label
188: if (graph.isSelectionEmpty())
189: infoLabel.setText(graph.getModel().getRootCount() + " "
190: + getString("Roots"));
191: else
192: infoLabel.setText(graph.getSelectionCount() + " "
193: + getString("Selected"));
194:
195: // Shows the editable state and zoom level in the respective labels
196: editableLabel
197: .setText((graph.isEditable()) ? getString("Editable")
198: : getString("ReadOnly"));
199: zoomLabel.setText((int) (graph.getScale() * 100) + "%");
200:
201: // Updates the modified label (file state)
202: JGraphEditorDiagramPane diagramPane = JGraphEditorDiagramPane
203: .getParentDiagramPane(graph);
204: if (diagramPane != null) {
205: JGraphEditorFile file = JGraphEditorModel
206: .getParentFile(diagramPane.getDiagram());
207: if (file != null) { // may be removed from file
208: modifiedLabel
209: .setText((file.isModified()) ? getString("Modified")
210: : (file.isNew()) ? getString("New")
211: : getString("Saved"));
212: }
213: }
214: }
215:
216: // Shows some memory usage details in the spare label
217: Runtime runtime = Runtime.getRuntime();
218: double freeMemory = runtime.freeMemory() / 1024;
219: double totalMemory = runtime.totalMemory() / 1024;
220: double usedMemory = totalMemory - freeMemory;
221:
222: spareLabel.setText(numberFormat.format(usedMemory)
223: + getString("KBFree") + " / "
224: + numberFormat.format(totalMemory)
225: + getString("KBTotal"));
226: }
227:
228: /**
229: * Provides a factory method to construct a status bar for an editor.
230: */
231: public static class FactoryMethod extends JGraphEditorFactoryMethod {
232:
233: /**
234: * Defines the default name for factory methods of this kind.
235: */
236: public static String NAME = "createStatusBar";
237:
238: /**
239: * References the enclosing editor.
240: */
241: protected JGraphEditor editor;
242:
243: /**
244: * Constructs a new factory method for the specified enclosing editor
245: * using {@link #NAME}.
246: *
247: * @param editor
248: * The editor that contains the factory method.
249: */
250: public FactoryMethod(JGraphEditor editor) {
251: super (NAME);
252: this .editor = editor;
253: }
254:
255: /*
256: * (non-Javadoc)
257: */
258: public Component createInstance(Node configuration) {
259: return new JGraphpadStatusBar(editor);
260: }
261:
262: }
263:
264: }
|