001: package net.sourceforge.squirrel_sql.plugins.graph;
002:
003: import net.sourceforge.squirrel_sql.client.session.ISession;
004: import net.sourceforge.squirrel_sql.plugins.graph.xmlbeans.FormatXmlBean;
005: import net.sourceforge.squirrel_sql.plugins.graph.xmlbeans.PrintXmlBean;
006: import net.sourceforge.squirrel_sql.plugins.graph.xmlbeans.ZoomerXmlBean;
007: import net.sourceforge.squirrel_sql.plugins.graph.graphtofiles.GraphToFilesCtrlr;
008: import net.sourceforge.squirrel_sql.plugins.graph.graphtofiles.SaveToFilePageFormat;
009:
010: import javax.swing.event.ChangeEvent;
011: import javax.swing.event.ChangeListener;
012: import java.awt.event.ActionEvent;
013: import java.awt.event.ActionListener;
014: import java.awt.event.ItemListener;
015: import java.awt.event.ItemEvent;
016: import java.awt.print.*;
017: import java.awt.image.BufferedImage;
018: import java.awt.*;
019: import java.util.ArrayList;
020:
021: public class ZoomPrintController {
022: Zoomer _zoomer;
023: private boolean _dontReactToSliderChanges = false;
024: private ISession _session;
025: private FormatController _formatController;
026: private EdgesListener _edgesListener;
027: private EdgesGraphComponent _edgesGraphComponent;
028: private GraphPlugin _plugin;
029: private GraphPrintable _printable;
030:
031: ZoomPrintPanel _panel = null;
032:
033: public ZoomPrintController(ZoomerXmlBean zoomerXmlBean,
034: PrintXmlBean printXmlBean, EdgesListener edgesListener,
035: GraphPrintable printable, ISession session,
036: GraphPlugin plugin) {
037: _printable = printable;
038: _plugin = plugin;
039:
040: _panel = new ZoomPrintPanel(new GraphPluginResources(_plugin));
041:
042: initZoom(session, zoomerXmlBean);
043: initPrint(printXmlBean, edgesListener);
044: }
045:
046: private void initPrint(PrintXmlBean printXmlBean,
047: EdgesListener edgesListener) {
048: _edgesListener = edgesListener;
049:
050: FormatControllerListener fcl = new FormatControllerListener() {
051: public void formatsChanged(FormatXmlBean selectedFormat) {
052: onFormatsChanged(selectedFormat);
053: }
054: };
055:
056: if (null != printXmlBean) {
057: _formatController = new FormatController(_session, _plugin,
058: fcl);
059: _panel.sldEdges.setValue(printXmlBean.getEdgesScale());
060: _panel.chkShowEdges.setSelected(printXmlBean.isShowEdges());
061: } else {
062: _formatController = new FormatController(_session, _plugin,
063: fcl);
064: }
065:
066: FormatXmlBean[] formats = _formatController.getFormats();
067:
068: FormatXmlBean toSelect = null;
069: for (int i = 0; i < formats.length; i++) {
070: _panel.cboFormat.addItem(formats[i]);
071: if (formats[i].isSelected()) {
072: toSelect = formats[i];
073: }
074: }
075: if (null != toSelect) {
076: _panel.cboFormat.setSelectedItem(toSelect);
077: }
078:
079: _panel.sldEdges.addChangeListener(new ChangeListener() {
080: public void stateChanged(ChangeEvent e) {
081: onSldEdgesChanged();
082: }
083: });
084:
085: _panel.chkShowEdges.addActionListener(new ActionListener() {
086: public void actionPerformed(ActionEvent e) {
087: onShowEdges();
088: }
089: });
090:
091: onShowEdges();
092:
093: _panel.btnPrint.addActionListener(new ActionListener() {
094: public void actionPerformed(ActionEvent e) {
095: onPrint();
096: }
097: });
098:
099: _panel.btnSaveImages.addActionListener(new ActionListener() {
100: public void actionPerformed(ActionEvent e) {
101: onSaveImages();
102: }
103: });
104:
105: _panel.cboFormat.addItemListener(new ItemListener() {
106: public void itemStateChanged(ItemEvent e) {
107: onSelectedFormatChanged(e);
108: }
109: });
110:
111: }
112:
113: private void onShowEdges() {
114: _panel.btnFormat.setEnabled(_panel.chkShowEdges.isSelected());
115: _panel.cboFormat.setEnabled(_panel.chkShowEdges.isSelected());
116: _panel.sldEdges.setEnabled(_panel.chkShowEdges.isSelected());
117: _panel.btnPrint.setEnabled(_panel.chkShowEdges.isSelected());
118:
119: fireEdgesGraphComponentChanged(_panel.chkShowEdges.isSelected()
120: && _panel.isVisible());
121: }
122:
123: private void onSelectedFormatChanged(ItemEvent e) {
124: if (ItemEvent.SELECTED == e.getStateChange()) {
125: fireEdgesGraphComponentChanged(_panel.chkShowEdges
126: .isSelected());
127: }
128: }
129:
130: private void onFormatsChanged(FormatXmlBean selectedFormat) {
131: FormatXmlBean[] formats = _formatController.getFormats();
132: _panel.cboFormat.removeAllItems();
133: for (int i = 0; i < formats.length; i++) {
134: _panel.cboFormat.addItem(formats[i]);
135: if (formats[i] == selectedFormat) {
136: formats[i].setSelected(true);
137: _panel.cboFormat.setSelectedItem(formats[i]);
138: } else {
139: formats[i].setSelected(false);
140: }
141: }
142: }
143:
144: private void onPrint() {
145: PrinterJob printJob = PrinterJob.getPrinterJob();
146:
147: PageFormat pf = initPrint(false);
148:
149: printJob.setPrintable(_printable, pf);
150: if (printJob.printDialog()) {
151: try {
152: printJob.print();
153: } catch (Exception ex) {
154: throw new RuntimeException(ex);
155: }
156: }
157: }
158:
159: private void onSaveImages() {
160: try {
161:
162: ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
163:
164: if (_panel.chkShowEdges.isSelected()) {
165:
166: PageFormat pf = initPrint(true);
167:
168: FormatXmlBean format = (FormatXmlBean) _panel.cboFormat
169: .getSelectedItem();
170:
171: PixelCalculater pc = new PixelCalculater(format);
172:
173: for (int pageIndex = 0;; ++pageIndex) {
174: int pxWidth = pc.getPixelWidth();
175: int pxHeight = pc.getPixelHeight();
176: BufferedImage img = prepareImage(pxWidth, pxHeight);
177: int pageState = _printable.print(img.getGraphics(),
178: pf, pageIndex);
179:
180: if (Printable.NO_SUCH_PAGE == pageState) {
181: break;
182: }
183:
184: images.add(img);
185: }
186: } else {
187: // No paper edges. We print the Graph as it is to a single image.
188:
189: Dimension graphPixelSize = _printable
190: .initPrintNoScaleSinglePage();
191: SaveToFilePageFormat pf = new SaveToFilePageFormat(
192: graphPixelSize);
193:
194: BufferedImage img = prepareImage(graphPixelSize.width,
195: graphPixelSize.height);
196: _printable.print(img.getGraphics(), pf, 0);
197:
198: images.add(img);
199: }
200:
201: new GraphToFilesCtrlr(images
202: .toArray(new BufferedImage[images.size()]),
203: _session.getApplication().getMainFrame());
204:
205: } catch (PrinterException e) {
206: throw new RuntimeException(e);
207: }
208: }
209:
210: private BufferedImage prepareImage(int pxWidth, int pxHeight) {
211: BufferedImage img = new BufferedImage(pxWidth, pxHeight,
212: BufferedImage.TYPE_INT_RGB);
213: img.getGraphics().setColor(Color.white);
214: img.getGraphics().fillRect(0, 0, pxWidth, pxHeight);
215: img.getGraphics().setColor(Color.black);
216: return img;
217: }
218:
219: private PageFormat initPrint(boolean isSaveToFile) {
220: FormatXmlBean format = (FormatXmlBean) _panel.cboFormat
221: .getSelectedItem();
222: _printable.initPrint(format.getWidth(), format.getHeight(),
223: _panel.sldEdges.getValue() / 100.0);
224:
225: PageFormat pf = isSaveToFile ? new SaveToFilePageFormat(format)
226: : new PageFormat();
227:
228: if (format.isLandscape()) {
229: pf.setOrientation(PageFormat.LANDSCAPE);
230: } else {
231: pf.setOrientation(PageFormat.PORTRAIT);
232: }
233:
234: return pf;
235: }
236:
237: private void fireEdgesGraphComponentChanged(boolean showEdges) {
238: if (null == _edgesGraphComponent) {
239: _edgesGraphComponent = new EdgesGraphComponent();
240: }
241:
242: if (showEdges) {
243: FormatXmlBean format = (FormatXmlBean) _panel.cboFormat
244: .getSelectedItem();
245: _edgesGraphComponent.init(format, _panel.sldEdges
246: .getValue() / 100.0, _panel.sldEdges
247: .getValueIsAdjusting());
248: _edgesListener.edgesGraphComponentChanged(
249: _edgesGraphComponent, true);
250:
251: } else {
252: _edgesListener.edgesGraphComponentChanged(
253: _edgesGraphComponent, false);
254: }
255:
256: }
257:
258: private void onSldEdgesChanged() {
259: fireEdgesGraphComponentChanged(_panel.chkShowEdges.isSelected());
260: }
261:
262: private void initZoom(ISession session, ZoomerXmlBean zoomerXmlBean) {
263: _session = session;
264: _zoomer = new Zoomer(zoomerXmlBean);
265:
266: _panel.setVisible(false);
267:
268: _panel.sldZoom.addChangeListener(new ChangeListener() {
269: public void stateChanged(ChangeEvent e) {
270: onSldZoomChanged();
271: }
272: });
273:
274: _panel.chkHideScrollBars
275: .addActionListener(new ActionListener() {
276: public void actionPerformed(ActionEvent e) {
277: onHideScrollbars();
278: }
279: });
280:
281: _panel.btnFormat.addActionListener(new ActionListener() {
282: public void actionPerformed(ActionEvent e) {
283: onBtnFormat();
284: }
285: });
286: }
287:
288: private void onBtnFormat() {
289: _formatController.setVisible(true);
290: }
291:
292: private void onHideScrollbars() {
293: _zoomer
294: .setHideScrollBars(_panel.chkHideScrollBars
295: .isSelected());
296: }
297:
298: private void onSldZoomChanged() {
299: if (_dontReactToSliderChanges) {
300: return;
301: }
302: _zoomer.setZoom(_panel.sldZoom.getValue() / 100.0,
303: _panel.sldZoom.getValueIsAdjusting());
304: }
305:
306: public ZoomPrintPanel getPanel() {
307: return _panel;
308: }
309:
310: public void setVisible(boolean b) {
311: _panel.setVisible(b);
312: _zoomer.setEnabled(b);
313: fireEdgesGraphComponentChanged(b);
314:
315: onShowEdges();
316:
317: try {
318: _dontReactToSliderChanges = true;
319: _panel.sldZoom
320: .setValue((int) (_zoomer.getZoom() * 100 + 0.5));
321: } finally {
322: _dontReactToSliderChanges = false;
323: }
324: }
325:
326: public Zoomer getZoomer() {
327: return _zoomer;
328: }
329:
330: public PrintXmlBean getPrintXmlBean() {
331: PrintXmlBean ret = new PrintXmlBean();
332: ret.setShowEdges(_panel.chkShowEdges.isSelected());
333: ret.setEdgesScale(_panel.sldEdges.getValue());
334:
335: return ret;
336: }
337:
338: public void sessionEnding() {
339: _formatController.close();
340: }
341: }
|