001: package net.sourceforge.squirrel_sql.plugins.graph;
002:
003: import net.sourceforge.squirrel_sql.client.session.ISession;
004: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.ObjectTreeNode;
005: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
006: import net.sourceforge.squirrel_sql.plugins.graph.xmlbeans.GraphControllerXmlBean;
007: import net.sourceforge.squirrel_sql.plugins.graph.xmlbeans.GraphXmlSerializer;
008: import net.sourceforge.squirrel_sql.plugins.graph.xmlbeans.TableFrameControllerXmlBean;
009:
010: import javax.swing.*;
011: import java.awt.*;
012: import java.awt.geom.Rectangle2D;
013: import java.util.Vector;
014:
015: public class GraphController {
016: private ISession _session;
017: private GraphMainPanelTab _graphPane;
018: private GraphDesktopController _desktopController;
019:
020: private Vector<TableFrameController> _openTableFrameCtrls = new Vector<TableFrameController>();
021: private TableFrameControllerListener _tableFrameControllerListener;
022: private static final int BORDER_X = ConstraintView.STUB_LENGTH + 10;
023: private static final int BORDER_Y = 10;
024: private AddTableListener _addTableListener;
025: private GraphDesktopListener _graphDesktopListener;
026: private GraphPlugin _plugin;
027: private GraphXmlSerializer _xmlSerializer;
028:
029: public GraphController(ISession session, GraphPlugin plugin,
030: GraphXmlSerializer xmlSerializer) {
031: _session = session;
032: _plugin = plugin;
033:
034: _graphDesktopListener = new GraphDesktopListener() {
035: public void saveGraphRequested() {
036: saveGraph();
037: }
038:
039: public void renameRequest(String newName) {
040: renameGraph(newName);
041: }
042:
043: public void removeRequest() {
044: removeGraph();
045: }
046:
047: public void refreshAllTablesRequested() {
048: refreshAllTables();
049: }
050:
051: public void scriptAllTablesRequested() {
052: scriptAllTables();
053: }
054:
055: };
056:
057: _desktopController = new GraphDesktopController(
058: _graphDesktopListener, _session, _plugin);
059: _graphPane = new GraphMainPanelTab(_desktopController);
060:
061: if (null == xmlSerializer) {
062: _xmlSerializer = new GraphXmlSerializer(_plugin, _session,
063: _graphPane, null);
064: } else {
065: _xmlSerializer = xmlSerializer;
066: }
067:
068: _tableFrameControllerListener = new TableFrameControllerListener() {
069: public void closed(TableFrameController tfc) {
070: onTableFrameControllerClosed(tfc);
071: }
072: };
073:
074: _addTableListener = new AddTableListener() {
075: public void addTablesRequest(String[] tablenames,
076: String schema, String catalog) {
077: onAddTablesRequest(tablenames, schema, catalog);
078: }
079: };
080:
081: GraphControllerXmlBean graphControllerXmlBean = null;
082: if (null != xmlSerializer) {
083: graphControllerXmlBean = _xmlSerializer.read();
084: _graphPane.setTitle(graphControllerXmlBean.getTitle());
085: _desktopController.initZoomer(graphControllerXmlBean
086: .getZoomerXmlBean(), graphControllerXmlBean
087: .getPrintXmlBean());
088: _desktopController
089: .setShowConstraintNames(graphControllerXmlBean
090: .isShowConstraintNames());
091: } else {
092: _graphPane.setTitle(_plugin.patchName(
093: _graphPane.getTitle(), _session));
094: _desktopController.initZoomer(null, null);
095: }
096:
097: _session.getSessionSheet().addMainTab(_graphPane);
098:
099: if (null != graphControllerXmlBean) {
100: TableFrameControllerXmlBean[] tableFrameControllerXmls = graphControllerXmlBean
101: .getTableFrameControllerXmls();
102: for (int i = 0; i < tableFrameControllerXmls.length; i++) {
103: addTableIntern(null, null, null, null,
104: tableFrameControllerXmls[i]);
105: }
106: }
107: }
108:
109: private void scriptAllTables() {
110: ITableInfo[] tableInfos = new ITableInfo[_openTableFrameCtrls
111: .size()];
112:
113: for (int i = 0; i < _openTableFrameCtrls.size(); i++) {
114: TableFrameController tableFrameController = _openTableFrameCtrls
115: .elementAt(i);
116: tableInfos[i] = tableFrameController.getTableInfo();
117: }
118:
119: SqlScriptAcessor.scriptTablesToSQLEntryArea(_session,
120: tableInfos);
121: }
122:
123: private void refreshAllTables() {
124: for (int i = 0; i < _openTableFrameCtrls.size(); i++) {
125: TableFrameController tableFrameController = _openTableFrameCtrls
126: .elementAt(i);
127: tableFrameController.refresh();
128: }
129: }
130:
131: private void removeGraph() {
132: _xmlSerializer.remove();
133: _session.getSessionSheet().removeMainTab(_graphPane);
134: _plugin.removeGraphController(this , _session);
135: }
136:
137: private void renameGraph(String newName) {
138: if (newName.equals(_graphPane.getTitle())) {
139: return;
140: }
141:
142: newName = _plugin.patchName(newName, _session);
143: _xmlSerializer.rename(newName);
144: int index = _session.getSessionSheet()
145: .removeMainTab(_graphPane);
146: _graphPane.setTitle(newName);
147: _session.getSessionSheet().insertMainTab(_graphPane, index);
148: saveGraph();
149: }
150:
151: public void saveGraph() {
152: GraphControllerXmlBean xmlBean = new GraphControllerXmlBean();
153: xmlBean.setTitle(_graphPane.getTitle());
154: xmlBean.setShowConstraintNames(_desktopController
155: .isShowConstraintNames());
156: xmlBean.setZoomerXmlBean(_desktopController.getZoomer()
157: .getXmlBean());
158: xmlBean.setPrintXmlBean(_desktopController
159: .getZoomPrintController().getPrintXmlBean());
160:
161: TableFrameControllerXmlBean[] frameXmls = new TableFrameControllerXmlBean[_openTableFrameCtrls
162: .size()];
163:
164: for (int i = 0; i < _openTableFrameCtrls.size(); i++) {
165: TableFrameController tableFrameController = _openTableFrameCtrls
166: .elementAt(i);
167: frameXmls[i] = tableFrameController.getXmlBean();
168: }
169: xmlBean.setTableFrameControllerXmls(frameXmls);
170:
171: _xmlSerializer.write(xmlBean);
172: }
173:
174: private void onAddTablesRequest(String[] tablenames, String schema,
175: String catalog) {
176: Point[] refCascadeIndent = new Point[1];
177: for (int i = 0; i < tablenames.length; i++) {
178: addTableIntern(refCascadeIndent, tablenames[i], schema,
179: catalog, null);
180: }
181: }
182:
183: private void onTableFrameControllerClosed(TableFrameController tfc) {
184: _openTableFrameCtrls.remove(tfc);
185: for (int i = 0; i < _openTableFrameCtrls.size(); i++) {
186: TableFrameController tableFrameController = _openTableFrameCtrls
187: .elementAt(i);
188: tableFrameController.tableFrameRemoved(tfc);
189: }
190:
191: }
192:
193: public void addTable(ObjectTreeNode selectedNode,
194: final Point[] refCascadeIndent) {
195: String catalog = selectedNode.getDatabaseObjectInfo()
196: .getCatalogName();
197: String schema = selectedNode.getDatabaseObjectInfo()
198: .getSchemaName();
199: String table = selectedNode.getDatabaseObjectInfo()
200: .getSimpleName();
201:
202: addTableIntern(refCascadeIndent, table, schema, catalog, null);
203: }
204:
205: private void addTableIntern(final Point[] refCascadeIndent,
206: String tableName, String schemaName, String catalogName,
207: final TableFrameControllerXmlBean xmlBean) {
208:
209: final TableFrameController tfc;
210:
211: if (null == xmlBean) {
212: tfc = new TableFrameController(_session,
213: _desktopController, _addTableListener, tableName,
214: schemaName, catalogName, null);
215: } else {
216: tfc = new TableFrameController(_session,
217: _desktopController, _addTableListener, null, null,
218: null, xmlBean);
219: }
220:
221: if (_openTableFrameCtrls.contains(tfc)) {
222: return;
223: }
224:
225: tfc
226: .addTableFrameControllerListener(_tableFrameControllerListener);
227:
228: SwingUtilities.invokeLater(new Runnable() {
229: public void run() {
230: _desktopController.addFrame(tfc.getFrame());
231: _openTableFrameCtrls.add(tfc);
232: initsAfterFrameAdded(tfc, refCascadeIndent,
233: null == xmlBean);
234: }
235: });
236:
237: }
238:
239: private void calcPosition(final TableFrameController tfc,
240: Point[] lastCascadePointRef) {
241: Dimension frmSize = tfc.getFrame().getSize();
242:
243: ///////////////////////////////////////////////////////////////////////////
244: // We try to find a completely free space for the new table frame.
245: for (int y = 0; y < _desktopController.getDesktopPane()
246: .getSize().getHeight(); y += frmSize.height + BORDER_Y) {
247: for (int x = 0; x < _desktopController.getDesktopPane()
248: .getSize().getWidth(); x += frmSize.width
249: + BORDER_X) {
250: Point leftUp = new Point(x, y);
251: Point rightDown = new Point(x + frmSize.width, y
252: + frmSize.height);
253:
254: if (isInDesktop(leftUp)
255: && isInDesktop(rightDown)
256: && false == isRectangleOccupied(leftUp,
257: rightDown, tfc)) {
258: tfc.getFrame().setBounds(leftUp.x, leftUp.y,
259: tfc.getFrame().getBounds().width,
260: tfc.getFrame().getBounds().height);
261: return;
262: }
263: }
264: }
265: //
266: //////////////////////////////////////////////////////////////////////////
267:
268: ////////////////////////////////////////////////////////////////////////////////
269: // We try to cascade
270: int cascadeIndent = tfc.getFrame().getTitlePane().getSize().height;
271:
272: if (null == lastCascadePointRef[0]) {
273: lastCascadePointRef[0] = new Point(cascadeIndent,
274: cascadeIndent);
275: } else {
276: lastCascadePointRef[0] = new Point(lastCascadePointRef[0].x
277: + cascadeIndent, lastCascadePointRef[0].y
278: + cascadeIndent);
279: }
280:
281: for (int x = lastCascadePointRef[0].x; x < _desktopController
282: .getDesktopPane().getSize().getWidth(); x += cascadeIndent) {
283: for (int y = lastCascadePointRef[0].y; y < _desktopController
284: .getDesktopPane().getSize().getHeight(); y += cascadeIndent) {
285: Point leftUp = new Point(x, y);
286: Point rightDown = new Point(x + frmSize.width, y
287: + frmSize.height);
288:
289: if (isInDesktop(leftUp) && isInDesktop(rightDown)) {
290: tfc.getFrame().setBounds(leftUp.x, leftUp.y,
291: tfc.getFrame().getBounds().width,
292: tfc.getFrame().getBounds().height);
293: lastCascadePointRef[0].y += cascadeIndent;
294: return;
295: }
296: }
297: lastCascadePointRef[0].y = cascadeIndent;
298: }
299: //
300: //////////////////////////////////////////////////////////////////////////////////
301:
302: // If we reach here we could not calculate a place to add the new table frame.
303: // Now it will automatically be added at (0,0).
304:
305: }
306:
307: private boolean isRectangleOccupied(Point leftUp, Point rightDown,
308: TableFrameController toExclude) {
309: for (int i = 0; i < _openTableFrameCtrls.size(); i++) {
310: TableFrameController tfc = _openTableFrameCtrls
311: .elementAt(i);
312:
313: if (tfc.equals(toExclude)) {
314: continue;
315: }
316:
317: Rectangle rectTfc = tfc.getFrame().getBounds();
318:
319: Rectangle rectParam = new Rectangle(leftUp.x - BORDER_X,
320: leftUp.y - BORDER_Y, rightDown.x - leftUp.x
321: + BORDER_X, rightDown.y - leftUp.y
322: + BORDER_Y);
323:
324: Rectangle2D interSect = rectParam
325: .createIntersection(rectTfc);
326:
327: if (0 < interSect.getWidth() && 0 < interSect.getHeight()) {
328: return true;
329: }
330: }
331: return false;
332:
333: }
334:
335: private boolean isInDesktop(Point p) {
336: if (_desktopController.getDesktopPane().getSize().width >= p.x
337: && _desktopController.getDesktopPane().getSize().height >= p.y) {
338: return true;
339: }
340: return false;
341: }
342:
343: private void initsAfterFrameAdded(TableFrameController tfc,
344: Point[] refCascadeIndent, boolean resetBounds) {
345: for (int i = 0; i < _openTableFrameCtrls.size(); i++) {
346: TableFrameController buf = _openTableFrameCtrls
347: .elementAt(i);
348: if (false == buf.equals(tfc)) {
349: buf.tableFrameOpen(tfc);
350: }
351: }
352:
353: Vector<TableFrameController> others = new Vector<TableFrameController>(
354: _openTableFrameCtrls);
355: others.remove(tfc);
356: TableFrameController[] othersArr = others
357: .toArray(new TableFrameController[others.size()]);
358: tfc.initAfterAddedToDesktop(othersArr, resetBounds);
359:
360: if (resetBounds) {
361: calcPosition(tfc, refCascadeIndent);
362: }
363:
364: }
365:
366: public String getTitle() {
367: return _graphPane.getTitle();
368: }
369:
370: public String toString() {
371: return getTitle();
372: }
373:
374: public void sessionEnding() {
375: _desktopController.sessionEnding();
376: }
377: }
|