// TODO: consider cleaning up this x and y business // it was added for copying/pasting widgets. // the regular loadWidget just uses the x and y from the string array // it passes in x=0, y=0 and we do a check. ugly, but works for now. // paste uses the x and y from the right click location. private Widget loadWidget(String[] strings, final String modelVersion, int x, int y) { Widget.LoadHelper helper = new Widget.LoadHelper() { public String version() { return modelVersion; } public String convert(String source, boolean reporter) { return workspace.autoConvert(source, true, reporter, modelVersion); } }; String type = strings[0]; if (x == 0) { x = Integer.parseInt(strings[1]); } if (y == 0) { y = Integer.parseInt(strings[2]); } if (viewWidget instanceof org.nlogo.window.ViewWidget && !type.equals("GRAPHICS-WINDOW") && VersionHistory.olderThan13pre1(modelVersion)) { y += ((org.nlogo.window.ViewWidget) viewWidget).getExtraHeight() + ((org.nlogo.window.ViewWidget) viewWidget).controlStrip.getHeight(); } if (type.equals("GRAPHICS-WINDOW")) { // the graphics widget (and the command center) are special cases because // they are not recreated at load time, but reused viewWidget.asWidget().load(strings, helper); // in 3D we don't add the viewWidget to the interface panel // so don't worry about all the sizing junk ev 7/5/07 java.awt.Container parent = viewWidget.asWidget().getParent(); if (parent != null) { parent.setSize(viewWidget.asWidget().getSize()); enforceMinimumAndMaximumWidgetSizes(viewWidget.asWidget()); parent.setLocation(x, y); zoomer.zoomWidgetLocation( getWrapper(viewWidget.asWidget()), true, true, 1.0, zoomer.zoomFactor()); zoomer.zoomWidgetSize( getWrapper(viewWidget.asWidget()), true, true, 1.0, zoomer.zoomFactor()); zoomer.scaleComponentFont( ((org.nlogo.window.ViewWidget) viewWidget).view, zoomer.zoomFactor(), 1.0, false); } return viewWidget.asWidget(); } else { Widget newGuy = null; newGuy = makeWidget(type, true); if (newGuy != null) { newGuy.load(strings, helper); enforceMinimumAndMaximumWidgetSizes(newGuy); addWidget(newGuy, x, y, false, true); } return newGuy; } }
public void handle(org.nlogo.window.Events.WidgetRemovedEvent e) { // since all plot widgets on the client are subordinate to // plot widgets on the server remove the plot widget // on the client when the plot in the server is removed // ev 1/18/07 if (e.widget instanceof org.nlogo.window.PlotWidget) { java.awt.Component[] comps = getComponents(); for (int i = 0; i < comps.length; i++) { if (comps[i] instanceof WidgetWrapper) { WidgetWrapper wrapper = (WidgetWrapper) comps[i]; Widget widget = wrapper.widget(); if (widget instanceof DummyPlotWidget && e.widget.displayName().equals(widget.displayName())) { removeWidget(wrapper); } } } repaint(); } }
WidgetWrapper(Widget widget, WidgetPanel interfacePanel) { this.widget = widget; this.interfacePanel = interfacePanel; setOpaque(true); verticallyResizable = computeVerticallyResizable(); horizontallyResizable = computeHorizontallyResizable(); topBar = new WindowBar(WindowBar.Type.TOP, BORDER_E, BORDER_W, verticallyResizable); leftBar = new WindowBar(WindowBar.Type.SIDE, 0, 0, horizontallyResizable); rightBar = new WindowBar(WindowBar.Type.SIDE, 0, 0, horizontallyResizable); bottomBar = new WindowBar(WindowBar.Type.BOTTOM, BORDER_E, BORDER_W, verticallyResizable); widgetChanged(); // update cornerHandles and otherwise make sure we are in good state -- CLB // don't let mouse events get through to the InterfacePanel // (is there a more elegant way to do this?) - ST 8/9/03 addMouseListener(new java.awt.event.MouseAdapter() {}); addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {}); glass = new javax.swing.JComponent() {}; glass.setOpaque(false); glass.addMouseListener(this); glass.addMouseMotionListener(this); widget.addPopupListeners(popupListener); setCursor(java.awt.Cursor.getDefaultCursor()); setLayout(null); setBackground(widget.getBackground()); // this is for notes, when the bg is transparent we don't want to see the // white widget wrapper. I don't know why setting the background to a // transparent color doesn't work. but it doesn't ev 6/8/07 setOpaque(widget.widgetWrapperOpaque()); add(glass, javax.swing.JLayeredPane.DRAG_LAYER); add(widget); add(topBar); add(leftBar); add(rightBar); add(bottomBar); doLayout(); }
public Widget loadWidget(String[] strings, final String modelVersion) { Widget.LoadHelper helper = new Widget.LoadHelper() { public String version() { return modelVersion; } public String convert(String source, boolean reporter) { return workspace.autoConvert(source, true, reporter, modelVersion); } }; String type = strings[0]; int x = Integer.parseInt(strings[1]); int y = Integer.parseInt(strings[2]); Widget newGuy = makeWidget(type, true); if (newGuy != null) { newGuy.load(strings, helper); enforceMinimumAndMaximumWidgetSizes(newGuy); addWidget(newGuy, x, y, false, true); } return newGuy; }
void doResize(int x, int y) { java.awt.Rectangle bounds = new java.awt.Rectangle(originalBounds); switch (mouseMode()) { case NW: y = StrictMath.max(y, BORDER_N - bounds.y); bounds.x += x; bounds.width -= x; bounds.y += y; bounds.height -= y; break; case NE: y = StrictMath.max(y, BORDER_N - bounds.y); bounds.width += x; bounds.y += y; bounds.height -= y; break; case SW: bounds.x += x; bounds.width -= x; bounds.height += y; break; case W: bounds.x += x; bounds.width -= x; break; case SE: bounds.width += x; bounds.height += y; break; case E: bounds.width += x; break; case S: bounds.height += y; break; case N: y = StrictMath.max(y, BORDER_N - bounds.y); bounds.y += y; bounds.height -= y; break; default: throw new IllegalStateException(); } if (interfacePanel.workspace.snapOn() && !interfacePanel.isZoomed()) { enforceGridSnapSize(bounds); } enforceMinimumSize(bounds); enforceMaximumSize(bounds); setBounds(widget.constrainDrag(bounds, originalBounds, mouseMode())); }
public InterfacePanel( org.nlogo.window.ViewWidgetInterface viewWidget, org.nlogo.window.GUIWorkspace workspace) { super(workspace); this.viewWidget = viewWidget; workspace.setWidgetContainer(this); // in 3d don't add the view widget since it's always // disabled there's no reason for it to take space 7/5/07 if (!Version.is3D()) { addWidget((Widget) viewWidget, 0, 0, false, false); } ((Widget) viewWidget).deleteable_$eq(false); addKeyListener(this); addMouseListener(this); }
protected WidgetWrapper addWidget( Widget widget, int x, int y, boolean select, boolean loadingWidget) { java.awt.Dimension size = widget.getSize(); WidgetWrapper wrapper = new WidgetWrapper(widget, this); wrapper.setVisible(false); // we need to add the wrapper before we can call wrapper.getPreferredSize(), because // that method looks at its parent and sees if it's an InterfacePanel // and zooms accordingly - ST 6/16/02 add(wrapper, DEFAULT_LAYER); moveToFront(wrapper); if (select || !loadingWidget) { wrapper.setSize(wrapper.getPreferredSize()); } else { wrapper.setSize(size); // //wrapper.getPreferredSize()); } if (workspace.snapOn() && !loadingWidget) { int gridX = (x / GRID_SNAP) * GRID_SNAP; int gridY = (y / GRID_SNAP) * GRID_SNAP; wrapper.setLocation(gridX, gridY); } else { wrapper.setLocation(x, y); } wrapper.validate(); wrapper.setVisible(true); zoomer.zoomWidget(wrapper, true, loadingWidget, 1.0, zoomer.zoomFactor()); if (select) { newWidget = wrapper; newWidget.originalBounds = newWidget.getBounds(); newWidget.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.CROSSHAIR_CURSOR)); } org.nlogo.log.Logger.logAddWidget(widget.classDisplayName(), widget.displayName()); return wrapper; }
public WidgetWrapper getWrapper(Widget widget) { return (WidgetWrapper) widget.getParent(); }
private java.awt.Point populateContextMenu( javax.swing.JPopupMenu menu, java.awt.Point p, java.awt.Component source) { java.awt.Point location = p; if (widget().getEditable() instanceof org.nlogo.api.Editable) { javax.swing.JMenuItem editItem = new javax.swing.JMenuItem("Edit..."); editItem.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { selected(true); foreground(); new org.nlogo.window.Events.EditWidgetEvent(null).raise(WidgetWrapper.this); } }); menu.add(editItem); } if (selected()) { javax.swing.JMenuItem unselectItem = new javax.swing.JMenuItem("Unselect"); unselectItem.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { selected(false); interfacePanel().setForegroundWrapper(); } }); menu.add(unselectItem); } else { javax.swing.JMenuItem selectItem = new javax.swing.JMenuItem("Select"); selectItem.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { selected(true); foreground(); } }); menu.add(selectItem); } if (widget().deleteable()) { javax.swing.JMenuItem deleteItem = new javax.swing.JMenuItem("Delete"); deleteItem.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { interfacePanel().deleteWidget(WidgetWrapper.this); } }); menu.add(new javax.swing.JPopupMenu.Separator()); menu.add(deleteItem); } if (widget().hasContextMenu()) { menu.add(new javax.swing.JPopupMenu.Separator()); location = widget().populateContextMenu(menu, p, source); if (widget().exportable()) { javax.swing.JMenuItem exportItem = new javax.swing.JMenuItem("Export..."); exportItem.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { try { String exportPath = org.nlogo.swing.FileDialog.show( widget(), "Export", java.awt.FileDialog.SAVE, interfacePanel .workspace() .guessExportName(widget.getDefaultExportName())); widget().export(exportPath); } catch (org.nlogo.awt.UserCancelException uce) { org.nlogo.util.Exceptions.ignore(uce); } } }); menu.add(exportItem); } widget.addExtraMenuItems(menu); } return location; }