001: package net.sourceforge.squirrel_sql.plugins.graph;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Component;
006: import java.awt.event.ActionEvent;
007: import java.awt.event.ActionListener;
008: import java.awt.event.InputEvent;
009: import java.awt.event.MouseAdapter;
010: import java.awt.event.MouseEvent;
011: import java.awt.event.MouseMotionAdapter;
012: import java.util.Vector;
013:
014: import javax.swing.JCheckBoxMenuItem;
015: import javax.swing.JInternalFrame;
016: import javax.swing.JMenuItem;
017: import javax.swing.JOptionPane;
018: import javax.swing.JPanel;
019: import javax.swing.JPopupMenu;
020: import javax.swing.JScrollPane;
021: import javax.swing.JSeparator;
022: import javax.swing.SwingUtilities;
023:
024: import net.sourceforge.squirrel_sql.client.session.ISession;
025: import net.sourceforge.squirrel_sql.fw.util.StringManager;
026: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
027: import net.sourceforge.squirrel_sql.plugins.graph.xmlbeans.PrintXmlBean;
028: import net.sourceforge.squirrel_sql.plugins.graph.xmlbeans.ZoomerXmlBean;
029:
030: public class GraphDesktopController {
031: private static final StringManager s_stringMgr = StringManagerFactory
032: .getStringManager(GraphDesktopController.class);
033:
034: private GraphDesktopPane _desktopPane;
035: private JScrollPane _scrollPane;
036: private ConstraintView _lastPressedConstraintView;
037:
038: private JPopupMenu _popUp;
039: private JMenuItem _mnuSaveGraph;
040: private JMenuItem _mnuRenameGraph;
041: private JMenuItem _mnuRemoveGraph;
042: private JMenuItem _mnuRefreshAllTables;
043: private JMenuItem _mnuScriptAllTables;
044: private JCheckBoxMenuItem _mnuShowConstraintNames;
045: private JCheckBoxMenuItem _mnuZoomPrint;
046: private GraphDesktopListener _listener;
047: private ISession _session;
048: private GraphPlugin _plugin;
049: private ZoomPrintController _zoomPrintController;
050: private JPanel _graphPanel;
051:
052: public GraphDesktopController(GraphDesktopListener listener,
053: ISession session, GraphPlugin plugin) {
054: _listener = listener;
055: _session = session;
056: _plugin = plugin;
057: _desktopPane = new GraphDesktopPane();
058: _desktopPane.setBackground(Color.white);
059:
060: _scrollPane = new JScrollPane(_desktopPane);
061:
062: _graphPanel = new JPanel(new BorderLayout());
063: _graphPanel.add(_scrollPane, BorderLayout.CENTER);
064:
065: _desktopPane.addMouseListener(new MouseAdapter() {
066: public void mouseClicked(MouseEvent e) {
067: onMouseClicked(e);
068: }
069:
070: public void mousePressed(MouseEvent e) {
071: onMousePressed(e);
072: }
073:
074: public void mouseReleased(MouseEvent e) {
075: onMouseReleased(e);
076: }
077: });
078:
079: _desktopPane.addMouseMotionListener(new MouseMotionAdapter() {
080: public void mouseDragged(MouseEvent e) {
081: onMouseDragged(e);
082: }
083: });
084:
085: createPopUp();
086:
087: }
088:
089: void initZoomer(ZoomerXmlBean zoomerXmlBean,
090: PrintXmlBean printXmlBean) {
091: EdgesListener edgesListener = new EdgesListener() {
092: public void edgesGraphComponentChanged(
093: EdgesGraphComponent edgesGraphComponent, boolean put) {
094: onEdgesGraphComponentChanged(edgesGraphComponent, put);
095: }
096: };
097:
098: _zoomPrintController = new ZoomPrintController(zoomerXmlBean,
099: printXmlBean, edgesListener, _desktopPane, _session,
100: _plugin);
101: _graphPanel.add(_zoomPrintController.getPanel(),
102: BorderLayout.SOUTH);
103: _mnuZoomPrint.setSelected(_zoomPrintController.getZoomer()
104: .isEnabled());
105: onZoomPrint();
106: }
107:
108: private void onEdgesGraphComponentChanged(
109: EdgesGraphComponent edgesGraphComponent, boolean put) {
110: if (put) {
111: _desktopPane
112: .putGraphComponents(new GraphComponent[] { edgesGraphComponent });
113: } else {
114: _desktopPane
115: .removeGraphComponents(new GraphComponent[] { edgesGraphComponent });
116: }
117: _desktopPane.repaint();
118: }
119:
120: private void createPopUp() {
121: _popUp = new JPopupMenu();
122:
123: // i18n[graph.saveGraph=Save graph]
124: _mnuSaveGraph = new JMenuItem(s_stringMgr
125: .getString("graph.saveGraph"));
126: _mnuSaveGraph.addActionListener(new ActionListener() {
127: public void actionPerformed(ActionEvent e) {
128: onSaveGraph();
129: }
130: });
131:
132: // i18n[graph.renameGraph=Rename graph]
133: _mnuRenameGraph = new JMenuItem(s_stringMgr
134: .getString("graph.renameGraph"));
135: _mnuRenameGraph.addActionListener(new ActionListener() {
136: public void actionPerformed(ActionEvent e) {
137: onRenameGraph();
138: }
139: });
140:
141: // i18n[graph.removeGraph=Remove graph]
142: _mnuRemoveGraph = new JMenuItem(s_stringMgr
143: .getString("graph.removeGraph"));
144: _mnuRemoveGraph.addActionListener(new ActionListener() {
145: public void actionPerformed(ActionEvent e) {
146: onRemoveGraph();
147: }
148: });
149:
150: // i18n[graph.refreshAllTables=Refresh all tables]
151: _mnuRefreshAllTables = new JMenuItem(s_stringMgr
152: .getString("graph.refreshAllTables"));
153: _mnuRefreshAllTables.addActionListener(new ActionListener() {
154: public void actionPerformed(ActionEvent e) {
155: onRefreshAllTables();
156: }
157: });
158:
159: // i18n[graph.scriptAllTables=Script all tables]
160: _mnuScriptAllTables = new JMenuItem(s_stringMgr
161: .getString("graph.scriptAllTables"));
162: _mnuScriptAllTables.addActionListener(new ActionListener() {
163: public void actionPerformed(ActionEvent e) {
164: onScriptAllTables();
165: }
166: });
167:
168: // i18n[graph.showConstr=Show constraint names]
169: _mnuShowConstraintNames = new JCheckBoxMenuItem(s_stringMgr
170: .getString("graph.showConstr"));
171: _mnuShowConstraintNames.addActionListener(new ActionListener() {
172: public void actionPerformed(ActionEvent e) {
173: _desktopPane.repaint();
174: }
175: });
176:
177: // i18n[graph.zoomPrint=Zoom/Print]
178: _mnuZoomPrint = new JCheckBoxMenuItem(s_stringMgr
179: .getString("graph.zoomPrint"));
180: _mnuZoomPrint.addActionListener(new ActionListener() {
181: public void actionPerformed(ActionEvent e) {
182: onZoomPrint();
183: }
184: });
185:
186: _popUp.add(_mnuSaveGraph);
187: _popUp.add(_mnuRenameGraph);
188: _popUp.add(_mnuRemoveGraph);
189: _popUp.add(new JSeparator());
190: _popUp.add(_mnuRefreshAllTables);
191: _popUp.add(_mnuScriptAllTables);
192: _popUp.add(new JSeparator());
193: _popUp.add(_mnuShowConstraintNames);
194: _popUp.add(_mnuZoomPrint);
195: }
196:
197: private void onScriptAllTables() {
198: _listener.scriptAllTablesRequested();
199: }
200:
201: private void onRefreshAllTables() {
202: _listener.refreshAllTablesRequested();
203: }
204:
205: private void onZoomPrint() {
206: _zoomPrintController.setVisible(_mnuZoomPrint.isSelected());
207: }
208:
209: private void onRemoveGraph() {
210: // i18n[graph.delGraph=Do you really wish to delete this graph?]
211: int res = JOptionPane.showConfirmDialog(_session
212: .getApplication().getMainFrame(), s_stringMgr
213: .getString("graph.delGraph"));
214: if (res == JOptionPane.YES_OPTION) {
215: _listener.removeRequest();
216: }
217: }
218:
219: private void onRenameGraph() {
220:
221: // i18n[graph.newName=Please enter a new name]
222: String newName = JOptionPane.showInputDialog(_session
223: .getApplication().getMainFrame(), s_stringMgr
224: .getString("graph.newName"));
225: if (null != newName && 0 != newName.trim().length()) {
226: _listener.renameRequest(newName);
227: }
228: }
229:
230: private void onSaveGraph() {
231: _listener.saveGraphRequested();
232: }
233:
234: private void maybeShowPopup(MouseEvent e) {
235: if (e.isPopupTrigger()) {
236: _popUp.show(e.getComponent(), e.getX(), e.getY());
237: }
238: }
239:
240: /**
241: * It's called put because it adds unique, like a Hashtable.
242: */
243: public void putConstraintViews(ConstraintView[] constraintViews) {
244: _desktopPane.putGraphComponents(constraintViews);
245: }
246:
247: public void removeConstraintViews(ConstraintView[] constraintViews,
248: boolean keepFoldingPoints) {
249: _desktopPane.removeGraphComponents(constraintViews);
250:
251: if (false == keepFoldingPoints) {
252: for (int i = 0; i < constraintViews.length; i++) {
253: constraintViews[i].removeAllFoldingPoints();
254: }
255: }
256: }
257:
258: private void refreshSelection(ConstraintView hitOne,
259: boolean allowDeselect) {
260: if (allowDeselect) {
261: hitOne.setSelected(!hitOne.isSelected());
262: } else if (false == hitOne.isSelected()) {
263: hitOne.setSelected(true);
264: }
265:
266: Vector<GraphComponent> graphComponents = _desktopPane
267: .getGraphComponents();
268:
269: for (int i = 0; i < graphComponents.size(); i++) {
270: if (false == graphComponents.elementAt(i) instanceof ConstraintView) {
271: continue;
272: }
273:
274: ConstraintView constraintView = (ConstraintView) graphComponents
275: .elementAt(i);
276: if (false == constraintView.equals(hitOne)) {
277: constraintView.setSelected(false);
278: }
279: }
280: }
281:
282: public void onMouseReleased(final MouseEvent e) {
283: _lastPressedConstraintView = null;
284:
285: ConstraintView hitOne = findHit(e);
286: if (null != hitOne) {
287: hitOne.mouseReleased(e);
288: } else {
289: maybeShowPopup(e);
290: }
291: }
292:
293: public void onMousePressed(final MouseEvent e) {
294: final ConstraintView hitOne = findHit(e);
295: if (null != hitOne) {
296: _lastPressedConstraintView = hitOne;
297:
298: if (InputEvent.BUTTON3_MASK == e.getModifiers()) {
299: refreshSelection(hitOne, false);
300: SwingUtilities.invokeLater(new Runnable() {
301: public void run() {
302: hitOne.mousePressed(e);
303: }
304: });
305: } else {
306: hitOne.mousePressed(e);
307: }
308: } else {
309: maybeShowPopup(e);
310: }
311: }
312:
313: public void onMouseClicked(final MouseEvent e) {
314: final ConstraintView hitOne = findHit(e);
315:
316: if (null != hitOne) {
317: refreshSelection(hitOne, InputEvent.BUTTON1_MASK == e
318: .getModifiers());
319: SwingUtilities.invokeLater(new Runnable() {
320: public void run() {
321: hitOne.mouseClicked(e);
322: }
323: });
324: }
325: }
326:
327: private void onMouseDragged(MouseEvent e) {
328: if (null != _lastPressedConstraintView) {
329: _lastPressedConstraintView.mouseDragged(e);
330: }
331: }
332:
333: private ConstraintView findHit(MouseEvent e) {
334: Vector<GraphComponent> graphComponents = _desktopPane
335: .getGraphComponents();
336:
337: for (int i = 0; i < graphComponents.size(); i++) {
338: GraphComponent graphComponent = graphComponents
339: .elementAt(i);
340:
341: if (graphComponent instanceof ConstraintView) {
342: ConstraintView constraintView = (ConstraintView) graphComponents
343: .elementAt(i);
344: if (constraintView.hitMe(e)) {
345: return constraintView;
346: }
347: }
348: }
349: return null;
350: }
351:
352: public void repaint() {
353: _desktopPane.repaint();
354: }
355:
356: public Component getGraphPanel() {
357: return _graphPanel;
358: }
359:
360: public void addFrame(JInternalFrame frame) {
361: _desktopPane.add(frame);
362: }
363:
364: public GraphDesktopPane getDesktopPane() {
365: return _desktopPane;
366: }
367:
368: public boolean isShowConstraintNames() {
369: return _mnuShowConstraintNames.isSelected();
370: }
371:
372: public void setShowConstraintNames(boolean showConstraintNames) {
373: _mnuShowConstraintNames.setSelected(showConstraintNames);
374: }
375:
376: public Zoomer getZoomer() {
377: return _zoomPrintController.getZoomer();
378: }
379:
380: public ZoomPrintController getZoomPrintController() {
381: return _zoomPrintController;
382: }
383:
384: public void sessionEnding() {
385: _zoomPrintController.sessionEnding();
386: }
387: }
|