1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
|
/*
* @(#)IconExample.java 1.0 28-SEPT-04
*
* Copyright (c) 2001-2004, Dean Mao All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. - Redistributions in
* binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution. - Neither the name of JGraph nor
* the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package org.jgraph.example;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.EventObject;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.AbstractCellEditor;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import org.jgraph.JGraph;
import org.jgraph.graph.CellView;
import org.jgraph.graph.CellViewRenderer;
import org.jgraph.graph.DefaultGraphCellEditor;
import org.jgraph.graph.GraphCellEditor;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.VertexView;
/**
* The cell view modifies the center point of the vertex such that it is
* relative to the icon rather than the entire vertex. The perimeter
*
* @author Dean Mao
* @created Sep 28, 2004
*/
public class JGraphIconView extends VertexView {
private transient MyMultiLinedEditor editor = new MyMultiLinedEditor();
protected static transient IconRenderer viewRenderer = new IconRenderer();
public JGraphIconView() {
super();
}
public JGraphIconView(Object cell) {
super(cell);
}
public GraphCellEditor getEditor() {
return editor;
}
private boolean isMouseOver;
/**
* The mouse over boolean is here so that we could potentially render the
* icon differently when the mouse is hovering above the vertex.
*
* @return whether or not the mouse is over the vertex
*/
public boolean isMouseOver() {
return isMouseOver;
}
public void setMouseOver(boolean isMouseOver) {
this.isMouseOver = isMouseOver;
}
/**
* The center point of the vertex should be in the middle of the icon
* instead of the middle of the icon/description duo. The reason we must
* calculate this is so that edges know where to point. We want to make the
* edges look like they are pointing to an icon from an icon.
*/
public Point2D getCenterPoint() {
Icon icon = GraphConstants.getIcon(getAllAttributes());
double iconWidth = icon.getIconWidth();
double iconHeight = icon.getIconHeight();
return new Point2D.Double(getBounds().getX() + iconWidth / 2,
getBounds().getY() + iconHeight / 2);
}
/**
* We don't want to calculate a perimeter point around the entire vertex.
* Only the perimeter around the icon will be calculated. This calculation
* is generic for elliptical perimeters.
*/
public Point2D getPerimeterPoint(Point2D source, Point2D p) {
Rectangle2D bounds = this.getBounds();
Icon icon = GraphConstants.getIcon(getAllAttributes());
double iconWidth = icon.getIconWidth();
double iconHeight = icon.getIconHeight();
double x = bounds.getX();
double y = bounds.getY();
double a = iconWidth / 2;
double b = iconHeight / 2;
double eccentricity = Math.sqrt(1 - ((b / a) * (b / a)));
double width = bounds.getWidth();
double height = viewRenderer.getIconDisplay().getPreferredSize()
.getHeight();
double xCenter = (x + (width / 2));
double yCenter = (y + (height / 2));
double dx = p.getX() - xCenter;
double dy = p.getY() - yCenter;
double theta = Math.atan2(dy, dx);
double eSquared = eccentricity * eccentricity;
double rPrime = a
* Math
.sqrt((1 - eSquared)
/ (1 - (eSquared * (Math.cos(theta) * Math
.cos(theta)))));
double ex = rPrime * Math.cos(theta);
double ey = rPrime * Math.sin(theta);
return new Point2D.Double(ex + xCenter, ey + yCenter);
}
public CellViewRenderer getRenderer() {
return viewRenderer;
}
class MyMultiLinedEditor extends MultiLinedEditor {
/**
* We offset the multi-lined editor by the height of the icon so that
* the multi-lined editor appears directly over the description text
* that we are editing.
*/
public Component getGraphCellEditorComponent(JGraph graph, Object cell,
boolean isSelected) {
Component component = super.getGraphCellEditorComponent(graph,
cell, isSelected);
Dimension dim = ((IconRenderer) JGraphIconView.this
.getRendererComponent(graph, false, false, false))
.getIconDisplay().getPreferredSize();
offsetY = (int) dim.getHeight();
return component;
}
}
/**
* This is a combination renderer that displays both an icon and a
* description under the icon. The description is html rendered so that it
* can be multi-lined.
*
* @author Dean Mao
* @created Sep 28, 2004
*/
public static class IconRenderer extends JComponent implements
CellViewRenderer {
private IconDisplay iconDisplay;
private DescriptionTextArea textRenderer;
public IconRenderer() {
super();
iconDisplay = new IconDisplay();
textRenderer = new DescriptionTextArea("text/html", "");
textRenderer.setOpaque(true);
iconDisplay.setOpaque(false);
GridBagLayout gbl = new GridBagLayout();
setLayout(gbl);
GridBagConstraints defaultRendererConstraint = new GridBagConstraints();
defaultRendererConstraint.gridx = 0;
defaultRendererConstraint.gridy = 0;
defaultRendererConstraint.gridwidth = 1;
defaultRendererConstraint.gridheight = 1;
defaultRendererConstraint.weightx = 1;
defaultRendererConstraint.weighty = 0;
defaultRendererConstraint.fill = GridBagConstraints.HORIZONTAL;
// add icon renderer:
gbl.setConstraints(iconDisplay, defaultRendererConstraint);
this.add(iconDisplay);
GridBagConstraints textRendererConstraint = new GridBagConstraints();
textRendererConstraint.gridx = 0;
textRendererConstraint.gridy = 1;
textRendererConstraint.gridwidth = 1;
textRendererConstraint.gridheight = GridBagConstraints.REMAINDER;
textRendererConstraint.weightx = 1;
textRendererConstraint.weighty = 1;
textRendererConstraint.fill = GridBagConstraints.BOTH;
textRendererConstraint.insets = new Insets(3, 3, 3, 3);
// add description renderer:
gbl.setConstraints(textRenderer, textRendererConstraint);
this.add(textRenderer);
}
public Dimension getPreferredSize() {
Dimension dim = super.getPreferredSize();
dim.setSize(dim.getWidth() + 40, dim.getHeight());
return dim;
}
private JGraphIconView view;
private boolean isSelected;
private boolean isFocused;
private boolean isPreview;
public java.awt.Component getRendererComponent(JGraph graph,
CellView view, boolean sel, boolean focus, boolean preview) {
if (view instanceof JGraphIconView) {
if (graph.getEditingCell() != view.getCell()) {
setBackground(Color.white);
}
this.view = (JGraphIconView) view;
this.isSelected = sel;
this.isFocused = focus;
this.isPreview = preview;
iconDisplay.setIcon(GraphConstants.getIcon(view
.getAllAttributes()));
textRenderer.setDescription(graph.convertValueToString(view));
return this;
}
return null;
}
public JGraphIconView getView() {
return this.view;
}
public boolean isSelected() {
return this.isSelected;
}
public boolean isFocused() {
return this.isFocused;
}
public boolean isPreview() {
return this.isPreview;
}
public IconDisplay getIconDisplay() {
return this.iconDisplay;
}
}
/**
* This JComponent only displays an icon as the upper part of the
* icon/description duo.
*
* @author Dean Mao
* @created Sep 28, 2004
*/
public static class IconDisplay extends JLabel {
public IconDisplay() {
super();
setVerticalAlignment(JLabel.CENTER);
setHorizontalAlignment(JLabel.CENTER);
setHorizontalTextPosition(JLabel.CENTER);
setVerticalTextPosition(JLabel.BOTTOM);
setFont(UIManager.getFont("Tree.font"));
setForeground(UIManager.getColor("Tree.textForeground"));
setBackground(UIManager.getColor("Tree.textBackground"));
}
public Dimension getMinimumSize() {
Dimension dim = super.getMinimumSize();
dim.setSize(dim.getWidth(), dim.getHeight() + 2);
return dim;
}
public Dimension getPreferredSize() {
return this.getMinimumSize();
}
public void paint(Graphics g) {
setBackground(Color.white);
setBorder(null);
// preview mode is "true" when we are dragging the component in the
// graph
if (!((IconRenderer) this.getParent()).isPreview()) {
// paint the icon only in preview mode.
super.paint(g);
} else {
// This is how we will paint the component when we are in
// preview
// mode (dragging component around).
g.setColor(Color.BLACK);
Dimension d = getSize();
g.drawOval((int) (d.width / 2 - 20), (int) (d.height / 2 - 20),
40, 40);
}
}
}
/**
* This JComponent only displays html renderered text as the lower part of
* the icon/description duo.
*
* @author Dean Mao
* @created Sep 28, 2004
*/
public static class DescriptionTextArea extends JEditorPane {
public DescriptionTextArea(String type, String text) {
super(type, text);
}
private String description;
public void setDescription(String description) {
this.description = description;
setText(this.description);
}
public void setText(String text) {
// display only the description if not blank
if (text != null && !text.equals("")) {
// make new lines appear as line breaks in the html renderered
// text
text = text.replaceAll("\n", "<br>");
super
.setText("<center><font color=\"#337733\" face=Arial size=-1>"
+ text + "</font></center>");
} else {
super.setText("");
}
}
}
public static class MultiLinedEditor extends DefaultGraphCellEditor {
public class RealCellEditor extends AbstractCellEditor implements
GraphCellEditor {
JTextArea editorComponent = new JTextArea();
public RealCellEditor() {
editorComponent.setBorder(UIManager
.getBorder("Tree.editorBorder"));
editorComponent.setLineWrap(true);
editorComponent.setWrapStyleWord(true);
// substitute a JTextArea's VK_ENTER action with our own that
// will stop an edit.
editorComponent.getInputMap(JComponent.WHEN_FOCUSED).put(
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
editorComponent.getInputMap(JComponent.WHEN_FOCUSED).put(
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
KeyEvent.SHIFT_DOWN_MASK), "shiftEnter");
editorComponent.getInputMap(JComponent.WHEN_FOCUSED).put(
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
KeyEvent.CTRL_DOWN_MASK), "metaEnter");
editorComponent.getInputMap(JComponent.WHEN_FOCUSED).put(
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
editorComponent.getActionMap().put("enter",
new AbstractAction() {
public void actionPerformed(ActionEvent e) {
stopCellEditing();
}
});
AbstractAction newLineAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
Document doc = editorComponent.getDocument();
try {
doc.insertString(
editorComponent.getCaretPosition(), "\n",
null);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
};
editorComponent.getActionMap().put("shiftEnter", newLineAction);
editorComponent.getActionMap().put("metaEnter", newLineAction);
}
public Component getGraphCellEditorComponent(JGraph graph,
Object value, boolean isSelected) {
editorComponent.setText(value.toString());
editorComponent.selectAll();
return editorComponent;
}
public Object getCellEditorValue() {
return editorComponent.getText();
}
public boolean stopCellEditing() {
// set the size of a vertex to that of an editor.
CellView view = graph.getGraphLayoutCache().getMapping(
graph.getEditingCell(), false);
Map map = view.getAllAttributes();
Rectangle2D cellBounds = GraphConstants.getBounds(map);
Rectangle editingBounds = editorComponent.getBounds();
GraphConstants.setBounds(map, new Rectangle((int) cellBounds
.getX(), (int) cellBounds.getY(), editingBounds.width,
editingBounds.height));
return super.stopCellEditing();
}
public boolean shouldSelectCell(EventObject event) {
editorComponent.requestFocus();
return super.shouldSelectCell(event);
}
}
public MultiLinedEditor() {
super();
}
/**
* Overriding this in order to set the size of an editor to that of an
* edited view.
*/
public Component getGraphCellEditorComponent(JGraph graph, Object cell,
boolean isSelected) {
Component component = super.getGraphCellEditorComponent(graph,
cell, isSelected);
// set the size of an editor to that of a view
CellView view = graph.getGraphLayoutCache().getMapping(cell, false);
Rectangle2D tmp = view.getBounds();
editingComponent.setBounds((int) tmp.getX(), (int) tmp.getY(),
(int) tmp.getWidth(), (int) tmp.getHeight());
// I have to set a font here instead of in the
// RealCellEditor.getGraphCellEditorComponent() because
// I don't know what cell is being edited when in the
// RealCellEditor.getGraphCellEditorComponent().
Font font = GraphConstants.getFont(view.getAllAttributes());
editingComponent.setFont((font != null) ? font : graph.getFont());
return component;
}
protected GraphCellEditor createGraphCellEditor() {
return new MultiLinedEditor.RealCellEditor();
}
/**
* Overriting this so that I could modify an eiditor container. see
* http://sourceforge.net/forum/forum.php?thread_id=781479&forum_id=140880
*/
protected Container createContainer() {
return new MultiLinedEditor.ModifiedEditorContainer();
}
class ModifiedEditorContainer extends EditorContainer {
public void doLayout() {
super.doLayout();
// substract 2 pixels that were added to the preferred size of
// the container for the border.
Dimension cSize = getSize();
Dimension dim = editingComponent.getSize();
editingComponent.setSize(dim.width - 2, dim.height);
// reset container's size based on a potentially new preferred
// size of a real editor.
setSize(cSize.width, getPreferredSize().height);
}
}
}
}
|