@Override public void resultChanged(LookupEvent ev) { if (!res.allInstances().isEmpty()) { if (scene != null) { // remove listener on previous scene scene.removeSceneListener(this); } scene = res.allInstances().iterator().next(); scene.addSceneListener(this); Lookup.Result<StrategicZoom> strategies = scene.getLookup().lookupResult(StrategicZoom.class); if (!strategies.allInstances().isEmpty()) { // set the flag bahwa sekarang ini mau ganti model. // by default, setModel akan trigger setItem dari first item di model // jadi kalo first item nya toFit, tiap kali pindah tab, bakal jadi toFit lagiPindahModel = true; zoomlist.setModel(new DefaultComboBoxModel(strategies.allInstances().toArray())); lagiPindahModel = false; } else { zoomlist.setModel(new DefaultComboBoxModel()); } ((ZoomComboBoxEditor) zoomlist.getEditor()).setZoomText(getCalibratedZoomFactor()); } else { if (scene != null) { // remove listener on previous scene scene.removeSceneListener(this); } } setEnabled(!res.allItems().isEmpty()); }
public static void main(String[] args) { final Scene scene = new Scene(); LayerWidget layer = new LayerWidget(scene); scene.addChild(layer); Widget nodeWidget = new Widget(scene); nodeWidget.setBorder(BorderFactory.createLineBorder(1, Color.RED)); nodeWidget.setPreferredLocation(new Point(100, 100)); layer.addChild(nodeWidget); final Widget deferredWidget = new Widget(scene); deferredWidget.setLayout(LayoutFactory.createCardLayout(deferredWidget)); deferredWidget.setBorder(BorderFactory.createLineBorder(1, Color.BLUE)); nodeWidget.addChild(deferredWidget); final Widget label = new LabelWidget(scene, "Click me to add ComponentWidget"); label.setBorder(BorderFactory.createLineBorder(1, Color.GREEN)); deferredWidget.addChild(label); LayoutFactory.setActiveCard(deferredWidget, label); label .getActions() .addAction( ActionFactory.createEditAction( new EditProvider() { public void edit(Widget widget) { ComponentWidget component = new ComponentWidget(scene, new JButton("This is the new ComponentWidget")); component.setBorder(BorderFactory.createLineBorder(1, Color.GREEN)); deferredWidget.addChild(component); LayoutFactory.setActiveCard(deferredWidget, component); } })); scene .getActions() .addAction( ActionFactory.createEditAction( new EditProvider() { public void edit(Widget widget) { LayoutFactory.setActiveCard(deferredWidget, label); } })); // to force the boundary // nodeWidget.setPreferredBounds (new Rectangle (0, 0, 70, 30)); // nodeWidget.setPreferredSize (new Dimension (70, 30)); nodeWidget.setLayout(LayoutFactory.createOverlayLayout()); nodeWidget.setCheckClipping(true); // SceneSupport.show(scene); }
private void setCalibratedZoomFactor(double d) { if (scene instanceof CalibratedScene) { CalibratedScene calibratedScene = (CalibratedScene) scene; calibratedScene.setCalibratedZoomFactor(d); } else { scene.setZoomFactor(d); } }
private double getCalibratedZoomFactor() { if (scene instanceof CalibratedScene) { CalibratedScene calibratedScene = (CalibratedScene) scene; return calibratedScene.getCalibratedZoomFactor(); } else { return scene.getZoomFactor(); } }
@Override public void unsetHovering(Widget widget) { if (widget != null) { widget.setBackground(scene.getLookFeel().getBackground(ObjectState.createNormal())); widget.setForeground(new Color(0, 0, 0, 0)); CustomPortInteractor port = (CustomPortInteractor) widget; port.setOver(false); } }
@Override public void setHovering(Widget widget) { if (widget != null) { ObjectState state = ObjectState.createNormal().deriveSelected(true); widget.setBackground(scene.getLookFeel().getBackground(state)); widget.setForeground(Color.yellow); CustomPortInteractor port = (CustomPortInteractor) widget; port.setOver(true); } }
public static void main(String[] args) { final Scene scene = new Scene(); scene.setBorder(BorderFactory.createEmptyBorder(10)); scene.setLayout( LayoutFactory.createVerticalFlowLayout(LayoutFactory.SerialAlignment.JUSTIFY, 10)); JTextField textField = new JTextField( "Text for editing - try to edit me. When the JTextField component is hidden, then the Widget just renders it."); final ComponentWidget textFieldWidget = new ComponentWidget(scene, textField); JToggleButton button = new JToggleButton( "Click to hide/show JTextField component bellow. The ComponentWidget is still in the scene and rendered."); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { textFieldWidget.setComponentVisible(!textFieldWidget.isComponentVisible()); scene.validate(); } }); scene.addChild(new ComponentWidget(scene, button)); scene.addChild(textFieldWidget); SeparatorWidget separator = new SeparatorWidget(scene, SeparatorWidget.Orientation.HORIZONTAL); scene.addChild(separator); JTextField textField2 = new JTextField("Text for editing - try to edit me."); final ComponentWidget textFieldWidget2 = new ComponentWidget(scene, textField2); JToggleButton button2 = new JToggleButton("Click to remove/add ComponentWidget from/to the scene."); button2.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (textFieldWidget2.getParentWidget() != null) scene.removeChild(textFieldWidget2); else scene.addChild(textFieldWidget2); scene.validate(); } }); scene.addChild(new ComponentWidget(scene, button2)); scene.addChild(textFieldWidget2); SceneSupport.show(scene); // TODO - call detach method on all ComponentWidget to prevent memory leaks }
/** * Takes the Scene and writes an image file according to the constraints defined by the caller. * This returns a BufferedImage of the Scene even if the file can not be written. * * @param visibleAreaOnly Eliminates all zoom features. If true, the exported image will be a * created from the visible area of the scene. * @param selectedOnly Create an image including only the objects selected on the scene. Note that * this feature requires that the scene is an instance of an ObjectScene since it is the * implementation that allows for object selection. * @param quality And integer value between 0-100. This is for JPG images only. Parameter is not * used if an image type other than jpg is selected. * @param width Directly sets the horizontal dimension of the exported image. This is only used * when the zoomType is ZoomType.CUSTOM_SIZE * @param height Directly sets the vertical dimension of the exported image. This is only used * when the zoomType is ZoomType.CUSTOM_SIZE. * @param createImageMap If true, the necessary steps are taken to setup the sequential call to * getSceneImageMapCoordinates. * @return image The raw image that was written to the file. * @throws java.io.IOException If for some reason the file cannot be written, an IOExeption will * be thrown. */ public BufferedImage createImage( ImageType imageType, ZoomType zoomType, boolean visibleAreaOnly, boolean selectedOnly, int quality, int width, int height, boolean createImageMap) throws IOException { double _scale = scene.getZoomFactor(); Rectangle sceneRec = scene.getPreferredBounds(); Rectangle viewRect = scene.getView().getVisibleRect(); BufferedImage bufferedImage; Graphics2D g; ArrayList<Widget> hiddenWidgets = new ArrayList<Widget>(); int _imageWidth = sceneRec.width; int _imageHeight = sceneRec.height; Set<?> _selectedObjects = null; if (selectedOnly) { // in order to use getSelectedObject the scene must be an // ObjectScene if (scene instanceof ObjectScene) { ObjectScene gScene = (ObjectScene) scene; // hide unselected widget HashSet<Object> invisible = new HashSet<Object>(); invisible.addAll(gScene.getObjects()); _selectedObjects = gScene.getSelectedObjects(); invisible.removeAll(_selectedObjects); for (Object o : invisible) { Widget widget = gScene.findWidget(o); if (widget != null && widget.isVisible()) { widget.setVisible(false); hiddenWidgets.add(widget); } } } } if (visibleAreaOnly) { _imageWidth = viewRect.width; _imageHeight = viewRect.height; } else { switch (zoomType) { case CUSTOM_SIZE: _imageWidth = width; _imageHeight = height; _scale = Math.min( (double) width / (double) sceneRec.width, (double) height / (double) sceneRec.height); break; case FIT_IN_WINDOW: _scale = Math.min( (double) viewRect.width / (double) sceneRec.width, (double) viewRect.height / (double) sceneRec.height); _imageWidth = (int) ((double) sceneRec.width * _scale); _imageHeight = (int) ((double) sceneRec.height * _scale); break; case CURRENT_ZOOM_LEVEL: _imageWidth = (int) (sceneRec.width * scene.getZoomFactor()); _imageHeight = (int) (sceneRec.height * scene.getZoomFactor()); break; case ACTUAL_SIZE: _imageWidth = sceneRec.width; _imageHeight = sceneRec.height; _scale = 1.0; break; } } // Note that the field variable are being set to method local variable. // This // is for the call to getSceneImageMapCoordinates that will come since // createImageMap is true. if (createImageMap) { this.selectedObjects = _selectedObjects; this.imageHeight = _imageHeight; this.imageWidth = _imageWidth; this.scale = _scale; } bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); g = bufferedImage.createGraphics(); g.translate(0, 0); g.scale(_scale, _scale); g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); scene.paint(g); // restore widget visibility for (Widget w : hiddenWidgets) { w.setVisible(true); } if (file != null) { FileImageOutputStream fo = new FileImageOutputStream(file); if (imageType == ImageType.JPG) { Iterator<?> iter = ImageIO.getImageWritersByFormatName("jpg"); ImageWriter writer = (ImageWriter) iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); if (quality > 100) { quality = 100; } if (quality < 0) { quality = 0; } iwp.setCompressionQuality(quality / 100); writer.setOutput(fo); IIOImage image = new IIOImage(bufferedImage, null, null); writer.write(null, image, iwp); writer.dispose(); } else { ImageIO.write(bufferedImage, "" + imageType, fo); } fo.flush(); fo.close(); } return bufferedImage; }
/** * Takes the Scene and writes an image file according to the constraints defined by the caller. * This returns a BufferedImage of the {@link Scene} even if the file can not be written. This * function creates the image for the entire {@link Scene}. * * @param visibleAreaOnly Eliminates all zoom features. If true, the exported image will be a * created from the visible area of the scene. * @param selectedOnly Create an image including only the objects selected on the scene. Note that * this feature requires that the scene is an instance of an ObjectScene since it is the * implementation that allows for object selection. * @param quality And integer value between 0-100. This is for JPG images only. Parameter is not * used if an image type other than jpg is selected. * @param canvas The {@link WorkareaCanvas} that is to exported. * @param createImageMap If true, the necessary steps are taken to setup the sequential call to * getSceneImageMapCoordinates. * @return image The raw image that was written to the file. * @throws java.io.IOException If for some reason the file cannot be written, an IOExeption will * be thrown. */ public BufferedImage createImage( ImageType imageType, ZoomType zoomType, boolean visibleAreaOnly, boolean selectedOnly, int quality, WorkareaCanvas canvas, boolean createImageMap) throws IOException { // Creates a rectangle with the starting point and the dimensions of the // scene seleceted for export Rectangle rectangle = canvas.getScene().getClientArea(); double _scale = scene.getZoomFactor(); Rectangle sceneRec = scene.getPreferredBounds(); Rectangle viewRect = scene.getView().getVisibleRect(); BufferedImage bufferedImage; Graphics2D g; ArrayList<Widget> hiddenWidgets = new ArrayList<Widget>(); int _imageWidth = sceneRec.width; int _imageHeight = sceneRec.height; Set<?> _selectedObjects = null; if (selectedOnly) { // in order to use getSelectedObject the scene must be an // ObjectScene if (scene instanceof ObjectScene) { ObjectScene gScene = (ObjectScene) scene; // hide unselected widget HashSet<Object> invisible = new HashSet<Object>(); invisible.addAll(gScene.getObjects()); _selectedObjects = gScene.getSelectedObjects(); invisible.removeAll(_selectedObjects); for (Object o : invisible) { Widget widget = gScene.findWidget(o); if (widget != null && widget.isVisible()) { widget.setVisible(false); hiddenWidgets.add(widget); } } } } if (visibleAreaOnly) { _imageWidth = viewRect.width; _imageHeight = viewRect.height; } else { switch (zoomType) { case CUSTOM_SIZE: _imageWidth = rectangle.width; _imageHeight = rectangle.height; _scale = Math.min( (double) rectangle.width / (double) sceneRec.width, (double) rectangle.height / (double) sceneRec.height); break; case FIT_IN_WINDOW: _scale = Math.min( (double) viewRect.width / (double) sceneRec.width, (double) viewRect.height / (double) sceneRec.height); _imageWidth = (int) ((double) sceneRec.width * _scale); _imageHeight = (int) ((double) sceneRec.height * _scale); break; case CURRENT_ZOOM_LEVEL: _imageWidth = (int) (sceneRec.width * scene.getZoomFactor()); _imageHeight = (int) (sceneRec.height * scene.getZoomFactor()); break; case ACTUAL_SIZE: _imageWidth = sceneRec.width; _imageHeight = sceneRec.height; _scale = 1.0; break; } } // Note that the field variable are being set to method local variable. // This // is for the call to getSceneImageMapCoordinates that will come since // createImageMap is true. if (createImageMap) { this.selectedObjects = _selectedObjects; this.imageHeight = _imageHeight; this.imageWidth = _imageWidth; this.scale = _scale; } bufferedImage = new BufferedImage(rectangle.width, rectangle.height, BufferedImage.TYPE_INT_RGB); g = bufferedImage.createGraphics(); g.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g.scale(_scale, _scale); g.setColor(Color.WHITE); g.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height); scene.paint(g); // restore widget visibility for (Widget w : hiddenWidgets) { w.setVisible(true); } if (file != null) { boolean save = true; if (file.exists()) { int answer = JOptionPane.showConfirmDialog( null, "This file exists. Do you wish to overwrite the file?", "Confirm", JOptionPane.YES_NO_OPTION); // The user has not pressed YES if (answer != 0) { save = false; } } if (save) { FileImageOutputStream fo = new FileImageOutputStream(file); if (imageType == ImageType.JPG) { Iterator<?> iter = ImageIO.getImageWritersByFormatName("jpg"); ImageWriter writer = (ImageWriter) iter.next(); ImageWriteParam iwp = writer.getDefaultWriteParam(); iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); if (quality > 100) { quality = 100; } if (quality < 0) { quality = 0; } iwp.setCompressionQuality(quality / 100); writer.setOutput(fo); IIOImage image = new IIOImage(bufferedImage, null, null); writer.write(null, image, iwp); writer.dispose(); } else { ImageIO.write(bufferedImage, "" + imageType, fo); } fo.flush(); fo.close(); } } return bufferedImage; }
/** * This is the master print call. All other print calls in this class pass through to here after * setting the appropriate parameters. Note that the scale type parameter take precedent over the * horizontal and vertical scaling percentages. Therefore, if the scale type was "SCALE_TO_FIT_Y", * then the values of horizontal and vertical scaling percentages would not be used. * * @param scene The Scene to be printed. * @param format format used for printing Scene. If null then a new default PageFormat is created. * @param scaleStrategy value representing the how to scale the printing. * @param scaleX Directly set the horizontal scale percentage. This parameter is only used when * the scale strategy is ScaleStrategy.SCALE_PERCENT. Otherwise it is ignored. * @param scaleY Directly set the vertical scale percentage. This parameter is only used when the * scale strategy is ScaleStrategy.SCALE_PERCENT. Otherwise it is ignored. * @param selectedOnly Print only the objects from the Scene that have been selected. Note that in * this case the Scene must be an instnace of an ObjectScene since this is required to * determine the selected objects. * @param visibleOnly Print only the object in the visible window. * @param region The rectangle representing the are of the Scene to be printed. * @param hiddenLayerWidgets Layer that are not to be printed. Might be used to hide the * background while printing. */ public static void print( Scene scene, PageFormat format, ScaleStrategy scaleStrategy, double scaleX, double scaleY, boolean selectedOnly, boolean visibleOnly, Rectangle region, List<LayerWidget> hiddenLayers) { // quick return if the scene is null if (scene == null) return; if (scaleStrategy == null) scaleStrategy = ScaleStrategy.NO_SCALING; PageableScene ps = new PageableScene( scene, format, scaleStrategy, scaleX, scaleY, selectedOnly, visibleOnly, region); PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPageable(ps); if (printJob.printDialog()) { try { // this can not go here because it hides all the widgets everytime. ArrayList<Widget> hiddenWidgets = new ArrayList<Widget>(); if (hiddenLayers != null && hiddenLayers.size() > 0) { for (Widget widget : hiddenLayers) { widget.setVisible(false); hiddenWidgets.add(widget); } } // if selectedOnly is true then we need to hide all the non-selected // widgets. Note that if the widgets were already hidden due to hiding // a layer, they are not hidden again. if (selectedOnly) { // in order to use getSelectedObject the scene must be an ObjectScene if (scene instanceof ObjectScene) { ObjectScene gScene = (ObjectScene) scene; // hide unselected widget HashSet<Object> invisible = new HashSet<Object>(); invisible.addAll(gScene.getObjects()); Set selectedObjects = gScene.getSelectedObjects(); invisible.removeAll(selectedObjects); for (Object o : invisible) { Widget widget = gScene.findWidget(o); if (widget != null && widget.isVisible()) { widget.setVisible(false); hiddenWidgets.add(widget); } } } } // Similar to the selectedOnly, if visibleOnly is true we have to // hide the widgets not in the visible window. The reason for this // is because setting the size alone does not hide the widgets. Any // page that is printed will include all its contained widgets if // they are not hidden. Note also that this must work with any // Scene, not just the ObjectScene. Therefore, we have to gather // all the widgets. if (visibleOnly || region != null) { // if the region is null, then the user is not trying to print a // region. The other way into this condition is if we need to // to print the visibleArea. So we set the region to be the // visible area. if (region == null) region = scene.getView().getVisibleRect(); List<Widget> allWidgets = new ArrayList<Widget>(); for (Widget widget : scene.getChildren()) { allWidgets.addAll(getAllNodeWidgets(widget, null)); } for (Widget widget : allWidgets) { Rectangle widgetInSceneCoordinates = widget.convertLocalToScene(widget.getBounds()); boolean included = region.contains(widgetInSceneCoordinates); if (!included && widget.isVisible()) { widget.setVisible(false); hiddenWidgets.add(widget); } } } printJob.print(); // restore widget visibility for (Widget w : hiddenWidgets) { w.setVisible(true); } scene.validate(); } catch (PrinterException pe) { System.out.println("Error printing: " + pe); } } }