All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
com.google.scrollview.ui.SVWindow Class Reference
Inheritance diagram for com.google.scrollview.ui.SVWindow:

Public Member Functions

void brush (int red, int green, int blue)
 
void brush (int red, int green, int blue, int alpha)
 
void clear ()
 
void createPolyline (int length)
 
void drawPolyline ()
 
 SVWindow (String name, int hash, int posX, int posY, int sizeX, int sizeY, int canvasSizeX, int canvasSizeY)
 
void addMessageBox ()
 
void setStrokeWidth (float width)
 
void drawEllipse (int x, int y, int width, int height)
 
void drawImage (PImage img, int xPos, int yPos)
 
void drawLine (int x1, int y1, int x2, int y2)
 
void drawRectangle (int x1, int y1, int x2, int y2)
 
void drawText (int x, int y, String text)
 
void pen (int red, int green, int blue)
 
void pen (int red, int green, int blue, int alpha)
 
void textAttributes (String font, int pixelSize, boolean bold, boolean italic, boolean underlined)
 
void zoomRectangle (int x1, int y1, int x2, int y2)
 
void update ()
 
void addMenuBarItem (String parent, String name, int id, boolean checked)
 
void addMenuBarItem (String parent, String name)
 
void addMenuBarItem (String parent, String name, int id)
 
void addMessage (String message)
 
void showInputDialog (String msg, String def, int id, SVEventType evtype)
 
void showInputDialog (String msg)
 
void showYesNoDialog (String msg)
 
void addPopupMenuItem (String parent, String name)
 
void addPopupMenuItem (String parent, String name, int cmdEvent, String value, String desc)
 
void destroy ()
 

Public Attributes

int hash
 
SVPopupMenu svPuMenu = null
 
PCanvas canvas
 

Static Public Attributes

static final double SCALING_FACTOR = 2
 
static int nrWindows = 0
 

Detailed Description

The SVWindow is the top-level ui class. It should get instantiated whenever the user intends to create a new window. It contains helper functions to draw on the canvas, add new menu items, show modal dialogs etc.

Author
wanke.nosp@m.@goo.nosp@m.gle.c.nosp@m.om

Definition at line 52 of file SVWindow.java.

Constructor & Destructor Documentation

com.google.scrollview.ui.SVWindow.SVWindow ( String  name,
int  hash,
int  posX,
int  posY,
int  sizeX,
int  sizeY,
int  canvasSizeX,
int  canvasSizeY 
)
inline

Construct a new SVWindow and set it visible.

Parameters
nameTitle of the window.
hashUnique internal representation. This has to be the same as defined by the client, as they use this to refer to the windows.
posXX position of where to draw the window (upper left).
posYY position of where to draw the window (upper left).
sizeXThe width of the window.
sizeYThe height of the window.
canvasSizeXThe canvas width of the window.
canvasSizeYThe canvas height of the window.

Definition at line 202 of file SVWindow.java.

203  {
204  super(name);
205 
206  // Provide defaults for sizes.
207  if (sizeX == 0) sizeX = canvasSizeX;
208  if (sizeY == 0) sizeY = canvasSizeY;
209  if (canvasSizeX == 0) canvasSizeX = sizeX;
210  if (canvasSizeY == 0) canvasSizeY = sizeY;
211 
212  // Initialize variables
213  nrWindows++;
214  this.hash = hash;
215  this.svEventHandler = new SVEventHandler(this);
216  this.currentPenColor = Color.BLACK;
217  this.currentBrushColor = Color.BLACK;
218  this.currentFont = new Font("Times New Roman", Font.PLAIN, 12);
219 
220  // Determine the initial size and zoom factor of the window.
221  // If the window is too big, rescale it and zoom out.
222  int shrinkfactor = 1;
223 
224  if (sizeX > MAX_WINDOW_X) {
225  shrinkfactor = (sizeX + MAX_WINDOW_X - 1) / MAX_WINDOW_X;
226  }
227  if (sizeY / shrinkfactor > MAX_WINDOW_Y) {
228  shrinkfactor = (sizeY + MAX_WINDOW_Y - 1) / MAX_WINDOW_Y;
229  }
230  winSizeX = sizeX / shrinkfactor;
231  winSizeY = sizeY / shrinkfactor;
232  double initialScalingfactor = 1.0 / shrinkfactor;
233  if (winSizeX > canvasSizeX || winSizeY > canvasSizeY) {
234  initialScalingfactor = Math.min(1.0 * winSizeX / canvasSizeX,
235  1.0 * winSizeY / canvasSizeY);
236  }
237 
238  // Setup the actual window (its size, camera, title, etc.)
239  if (canvas == null) {
240  canvas = new PCanvas();
241  getContentPane().add(canvas, BorderLayout.CENTER);
242  }
243 
244  layer = canvas.getLayer();
245  canvas.setBackground(Color.BLACK);
246 
247  // Disable anitaliasing to make the lines more visible.
248  canvas.setDefaultRenderQuality(PPaintContext.LOW_QUALITY_RENDERING);
249 
250  setLayout(new BorderLayout());
251 
252  setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
253 
254  validate();
255  canvas.requestFocus();
256 
257  // Manipulation of Piccolo's scene graph should be done from Swings
258  // event dispatch thread since Piccolo is not thread safe. This code calls
259  // initialize() from that thread once the PFrame is initialized, so you are
260  // safe to start working with Piccolo in the initialize() method.
261  SwingUtilities.invokeLater(new Runnable() {
262  public void run() {
263  repaint();
264  }
265  });
266 
267  setSize(winSizeX, winSizeY);
268  setLocation(posX, posY);
269  setTitle(name);
270 
271  // Add a Scrollpane to be able to scroll within the canvas
272  PScrollPane scrollPane = new PScrollPane(canvas);
273  getContentPane().add(scrollPane);
274  scrollPane.setWheelScrollingEnabled(false);
275  PCamera lc = canvas.getCamera();
276  lc.scaleViewAboutPoint(initialScalingfactor, 0, 0);
277 
278  // Disable the default event handlers and add our own.
279  addWindowListener(svEventHandler);
280  canvas.removeInputEventListener(canvas.getPanEventHandler());
281  canvas.removeInputEventListener(canvas.getZoomEventHandler());
282  canvas.addInputEventListener(svEventHandler);
283  canvas.addKeyListener(svEventHandler);
284 
285  // Make the window visible.
286  validate();
287  setVisible(true);
288 
289  }
name_table name

Member Function Documentation

void com.google.scrollview.ui.SVWindow.addMenuBarItem ( String  parent,
String  name,
int  id,
boolean  checked 
)
inline

Adds a checkbox entry to the menubar, c.f. SVMenubar.add(...)

Definition at line 501 of file SVWindow.java.

502  {
503  svMenuBar.add(parent, name, id, checked);
504  }
name_table name
void add(String parent, String name, int id)
Definition: SVMenuBar.java:73
void com.google.scrollview.ui.SVWindow.addMenuBarItem ( String  parent,
String  name 
)
inline

Adds a submenu to the menubar, c.f. SVMenubar.add(...)

Definition at line 507 of file SVWindow.java.

507  {
508  addMenuBarItem(parent, name, -1);
509  }
void addMenuBarItem(String parent, String name, int id, boolean checked)
Definition: SVWindow.java:501
name_table name
void com.google.scrollview.ui.SVWindow.addMenuBarItem ( String  parent,
String  name,
int  id 
)
inline

Adds a new entry to the menubar, c.f. SVMenubar.add(...)

Definition at line 512 of file SVWindow.java.

512  {
513  if (svMenuBar == null) {
514  svMenuBar = new SVMenuBar(this);
515 
516  }
517  svMenuBar.add(parent, name, id);
518  }
name_table name
void add(String parent, String name, int id)
Definition: SVMenuBar.java:73
void com.google.scrollview.ui.SVWindow.addMessage ( String  message)
inline

Add a message to the message box.

Definition at line 521 of file SVWindow.java.

521  {
522  if (ta != null) {
523  ta.append(message + "\n");
524  } else {
525  System.out.println(message + "\n");
526  }
527  }
void com.google.scrollview.ui.SVWindow.addMessageBox ( )
inline

Convenience function to add a message box to the window which can be used to output debug information.

Definition at line 295 of file SVWindow.java.

295  {
296  if (ta == null) {
297  ta = new TextArea();
298  ta.setEditable(false);
299  getContentPane().add(ta, BorderLayout.SOUTH);
300  }
301  // We need to make the window bigger to accomodate the message box.
302  winSizeY += DEF_MESSAGEBOX_HEIGHT;
303  setSize(winSizeX, winSizeY);
304  }
void com.google.scrollview.ui.SVWindow.addPopupMenuItem ( String  parent,
String  name 
)
inline

Adds a submenu to the popup menu, c.f. SVPopupMenu.add(...)

Definition at line 617 of file SVWindow.java.

617  {
618  if (svPuMenu == null) {
619  svPuMenu = new SVPopupMenu(this);
620  }
621  svPuMenu.add(parent, name, -1);
622  }
void add(String parent, String name, int id)
name_table name
void com.google.scrollview.ui.SVWindow.addPopupMenuItem ( String  parent,
String  name,
int  cmdEvent,
String  value,
String  desc 
)
inline

Adds a new menu entry to the popup menu, c.f. SVPopupMenu.add(...)

Definition at line 625 of file SVWindow.java.

626  {
627  if (svPuMenu == null) {
628  svPuMenu = new SVPopupMenu(this);
629  }
630  svPuMenu.add(parent, name, cmdEvent, value, desc);
631  }
void add(String parent, String name, int id)
name_table name
void com.google.scrollview.ui.SVWindow.brush ( int  red,
int  green,
int  blue 
)
inline

Set the brush to an RGB color

Definition at line 118 of file SVWindow.java.

118  {
119  brush(red, green, blue, 255);
120  }
void brush(int red, int green, int blue)
Definition: SVWindow.java:118
void com.google.scrollview.ui.SVWindow.brush ( int  red,
int  green,
int  blue,
int  alpha 
)
inline

Set the brush to an RGBA color

Definition at line 123 of file SVWindow.java.

123  {
124  // If alpha is zero, use a null brush to save rendering time.
125  if (alpha == 0) {
126  currentBrushColor = null;
127  } else {
128  currentBrushColor = new Color(red, green, blue, alpha);
129  }
130  }
void com.google.scrollview.ui.SVWindow.clear ( )
inline

Erase all content from the window, but do not destroy it.

Definition at line 133 of file SVWindow.java.

133  {
134  // Manipulation of Piccolo's scene graph should be done from Swings
135  // event dispatch thread since Piccolo is not thread safe. This code calls
136  // removeAllChildren() from that thread and releases the latch.
137  final java.util.concurrent.CountDownLatch latch = new java.util.concurrent.CountDownLatch(1);
138  SwingUtilities.invokeLater(new Runnable() {
139  public void run() {
140  layer.removeAllChildren();
141  repaint();
142  latch.countDown();
143  }
144  });
145  try {
146  latch.await();
147  } catch (InterruptedException e) {
148  }
149  }
void com.google.scrollview.ui.SVWindow.createPolyline ( int  length)
inline

Start setting up a new polyline. The server will now expect polyline data until the polyline is complete.

Parameters
lengthnumber of coordinate pairs

Definition at line 157 of file SVWindow.java.

157  {
158  ScrollView.polylineXCoords = new float[length];
159  ScrollView.polylineYCoords = new float[length];
160  ScrollView.polylineSize = length;
161  ScrollView.polylineScanned = 0;
162  }
void com.google.scrollview.ui.SVWindow.destroy ( )
inline

Destroys a window.

Definition at line 634 of file SVWindow.java.

634  {
635  ScrollView.addMessage(new SVEvent(SVEventType.SVET_DESTROY, this, 0,
636  "SVET_DESTROY"));
637  setVisible(false);
638  // dispose();
639  }
SVEventType
Definition: scrollview.h:45
void com.google.scrollview.ui.SVWindow.drawEllipse ( int  x,
int  y,
int  width,
int  height 
)
inline

Draw an ellipse at (x,y) with given width and height, using the current stroke, the current brush color to fill it and the current pen color for the outline.

Definition at line 322 of file SVWindow.java.

322  {
323  PPath pn = PPath.createEllipse(x, y, width, height);
324  pn.setStrokePaint(currentPenColor);
325  pn.setStroke(stroke);
326  pn.setPaint(currentBrushColor);
327  layer.addChild(pn);
328  }
void com.google.scrollview.ui.SVWindow.drawImage ( PImage  img,
int  xPos,
int  yPos 
)
inline

Draw the image with the given name at (x,y). Any image loaded stays in memory, so if you intend to redraw an image, you do not have to use createImage again.

Definition at line 335 of file SVWindow.java.

335  {
336  img.setX(xPos);
337  img.setY(yPos);
338  layer.addChild(img);
339  }
void com.google.scrollview.ui.SVWindow.drawLine ( int  x1,
int  y1,
int  x2,
int  y2 
)
inline

Draw a line from (x1,y1) to (x2,y2) using the current pen color and stroke.

Definition at line 344 of file SVWindow.java.

344  {
345  PPath pn = PPath.createLine(x1, y1, x2, y2);
346  pn.setStrokePaint(currentPenColor);
347  pn.setPaint(null); // Null paint may render faster than the default.
348  pn.setStroke(stroke);
349  pn.moveTo(x1, y1);
350  pn.lineTo(x2, y2);
351  layer.addChild(pn);
352  }
void com.google.scrollview.ui.SVWindow.drawPolyline ( )
inline

Draw the now complete polyline.

Definition at line 167 of file SVWindow.java.

167  {
168  int numCoords = ScrollView.polylineXCoords.length;
169  if (numCoords < 2) {
170  return;
171  }
172  PPath pn = PPath.createLine(ScrollView.polylineXCoords[0],
173  ScrollView.polylineYCoords[0],
174  ScrollView.polylineXCoords[1],
175  ScrollView.polylineYCoords[1]);
176  pn.reset();
177  pn.moveTo(ScrollView.polylineXCoords[0], ScrollView.polylineYCoords[0]);
178  for (int p = 1; p < numCoords; ++p) {
179  pn.lineTo(ScrollView.polylineXCoords[p], ScrollView.polylineYCoords[p]);
180  }
181  pn.closePath();
182  ScrollView.polylineSize = 0;
183  pn.setStrokePaint(currentPenColor);
184  pn.setPaint(null); // Don't fill the polygon - this is just a polyline.
185  pn.setStroke(stroke);
186  layer.addChild(pn);
187  }
void com.google.scrollview.ui.SVWindow.drawRectangle ( int  x1,
int  y1,
int  x2,
int  y2 
)
inline

Draw a rectangle given the two points (x1,y1) and (x2,y2) using the current stroke, pen color for the border and the brush to fill the interior.

Definition at line 359 of file SVWindow.java.

359  {
360 
361  if (x1 > x2) {
362  int t = x1;
363  x1 = x2;
364  x2 = t;
365  }
366  if (y1 > y2) {
367  int t = y1;
368  y1 = y2;
369  y2 = t;
370  }
371 
372  PPath pn = PPath.createRectangle(x1, y1, x2 - x1, y2 - y1);
373  pn.setStrokePaint(currentPenColor);
374  pn.setStroke(stroke);
375  pn.setPaint(currentBrushColor);
376  layer.addChild(pn);
377  }
void com.google.scrollview.ui.SVWindow.drawText ( int  x,
int  y,
String  text 
)
inline

Draw some text at (x,y) using the current pen color and text attributes. If the current font does NOT support at least one character, it tries to find a font which is capable of displaying it and use that to render the text. Note: If the font says it can render a glyph, but in reality it turns out to be crap, there is nothing we can do about it.

Definition at line 386 of file SVWindow.java.

386  {
387  int unreadableCharAt = -1;
388  char[] chars = text.toCharArray();
389  PText pt = new PText(text);
390  pt.setTextPaint(currentPenColor);
391  pt.setFont(currentFont);
392 
393  // Check to see if every character can be displayed by the current font.
394  for (int i = 0; i < chars.length; i++) {
395  if (!currentFont.canDisplay(chars[i])) {
396  // Set to the first not displayable character.
397  unreadableCharAt = i;
398  break;
399  }
400  }
401 
402  // Have to find some working font and use it for this text entry.
403  if (unreadableCharAt != -1) {
404  Font[] allfonts =
405  GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
406  for (int j = 0; j < allfonts.length; j++) {
407  if (allfonts[j].canDisplay(chars[unreadableCharAt])) {
408  Font tempFont =
409  new Font(allfonts[j].getFontName(), currentFont.getStyle(),
410  currentFont.getSize());
411  pt.setFont(tempFont);
412  break;
413  }
414  }
415  }
416 
417  pt.setX(x);
418  pt.setY(y);
419  layer.addChild(pt);
420  }
void com.google.scrollview.ui.SVWindow.pen ( int  red,
int  green,
int  blue 
)
inline

Set the pen color to an RGB value

Definition at line 423 of file SVWindow.java.

423  {
424  pen(red, green, blue, 255);
425  }
void pen(int red, int green, int blue)
Definition: SVWindow.java:423
void com.google.scrollview.ui.SVWindow.pen ( int  red,
int  green,
int  blue,
int  alpha 
)
inline

Set the pen color to an RGBA value

Definition at line 428 of file SVWindow.java.

428  {
429  currentPenColor = new Color(red, green, blue, alpha);
430  }
void com.google.scrollview.ui.SVWindow.setStrokeWidth ( float  width)
inline

Allows you to specify the thickness with which to draw lines, recantgles and ellipses.

Parameters
widthThe new thickness.

Definition at line 311 of file SVWindow.java.

311  {
312  // If this worked we wouldn't need the antialiased rendering off.
313  // stroke = new PFixedWidthStroke(width);
314  stroke = new BasicStroke(width);
315  }
void com.google.scrollview.ui.SVWindow.showInputDialog ( String  msg,
String  def,
int  id,
SVEventType  evtype 
)
inline

Show a modal input dialog. The answer by the dialog is then send to the client, together with the associated menu id, as SVET_POPUP

Parameters
msgThe text that is displayed in the dialog.
defThe default value of the dialog.
idThe associated commandId
evtypeThe event this is associated with (usually SVET_MENU or SVET_POPUP)

Definition at line 569 of file SVWindow.java.

570  {
571  svEventHandler.timer.stop();
572  String tmp =
573  (String) JOptionPane.showInputDialog(this, msg, "",
574  JOptionPane.QUESTION_MESSAGE, null, null, def);
575 
576  if (tmp != null) {
577  tmp = convertIntegerStringToUnicodeString(tmp);
578  SVEvent res = new SVEvent(evtype, this, id, tmp);
579  ScrollView.addMessage(res);
580  }
581  svEventHandler.timer.restart();
582  }
void com.google.scrollview.ui.SVWindow.showInputDialog ( String  msg)
inline

Shows a modal input dialog to the user. The return value is automatically sent to the client as SVET_INPUT event (with command id -1).

Parameters
msgThe text of the dialog.

Definition at line 591 of file SVWindow.java.

591  {
592  showInputDialog(msg, null, -1, SVEventType.SVET_INPUT);
593  }
SVEventType
Definition: scrollview.h:45
void showInputDialog(String msg, String def, int id, SVEventType evtype)
Definition: SVWindow.java:569
void com.google.scrollview.ui.SVWindow.showYesNoDialog ( String  msg)
inline

Shows a dialog presenting "Yes" and "No" as answers and returns either a "y" or "n" to the client.

Parameters
msgThe text that is displayed in the dialog.

Definition at line 601 of file SVWindow.java.

601  {
602  // res returns 0 on yes, 1 on no. Seems to be a bit counterintuitive
603  int res =
604  JOptionPane.showOptionDialog(this, msg, "", JOptionPane.YES_NO_OPTION,
605  JOptionPane.QUESTION_MESSAGE, null, null, null);
606  SVEvent e = null;
607 
608  if (res == 0) {
609  e = new SVEvent(SVEventType.SVET_INPUT, this, 0, 0, 0, 0, "y");
610  } else if (res == 1) {
611  e = new SVEvent(SVEventType.SVET_INPUT, this, 0, 0, 0, 0, "n");
612  }
613  ScrollView.addMessage(e);
614  }
SVEventType
Definition: scrollview.h:45
void com.google.scrollview.ui.SVWindow.textAttributes ( String  font,
int  pixelSize,
boolean  bold,
boolean  italic,
boolean  underlined 
)
inline

Define how to display text. Note: underlined is not currently not supported

Definition at line 435 of file SVWindow.java.

436  {
437 
438  // For legacy reasons convert "Times" to "Times New Roman"
439  if (font.equals("Times")) {
440  font = "Times New Roman";
441  }
442 
443  int style = Font.PLAIN;
444  if (bold) {
445  style += Font.BOLD;
446  }
447  if (italic) {
448  style += Font.ITALIC;
449  }
450  currentFont = new Font(font, style, pixelSize);
451  }
void com.google.scrollview.ui.SVWindow.update ( )
inline

Flush buffers and update display.

Only actually reacts if there are no more messages in the stack, to prevent the canvas from flickering.

Definition at line 483 of file SVWindow.java.

483  {
484  // TODO(rays) fix bugs in piccolo or use something else.
485  // The repaint function generates many
486  // exceptions for no good reason. We catch and ignore as many as we
487  // can here, but most of them are generated by the system repaints
488  // caused by resizing/exposing parts of the window etc, and they
489  // generate unwanted stack traces that have to be piped to /dev/null
490  // (on linux).
491  try {
492  repaint();
493  } catch (NullPointerException e) {
494  // Do nothing so the output isn't full of stack traces.
495  } catch (IllegalPathStateException e) {
496  // Do nothing so the output isn't full of stack traces.
497  }
498  }
void com.google.scrollview.ui.SVWindow.zoomRectangle ( int  x1,
int  y1,
int  x2,
int  y2 
)
inline

Zoom the window to the rectangle given the two points (x1,y1) and (x2,y2), which must be greater than (x1,y1).

Definition at line 457 of file SVWindow.java.

457  {
458  if (x2 > x1 && y2 > y1) {
459  winSizeX = getWidth();
460  winSizeY = getHeight();
461  int width = x2 - x1;
462  int height = y2 - y1;
463  // Since piccolo doesn't do this well either, pad with a margin
464  // all the way around.
465  int wmargin = width / 2;
466  int hmargin = height / 2;
467  double scalefactor = Math.min(winSizeX / (2.0 * wmargin + width),
468  winSizeY / (2.0 * hmargin + height));
469  PCamera lc = canvas.getCamera();
470  lc.scaleView(scalefactor / lc.getViewScale());
471  lc.animateViewToPanToBounds(new Rectangle(x1 - hmargin, y1 - hmargin,
472  2 * wmargin + width,
473  2 * hmargin + height), 0);
474  }
475  }

Member Data Documentation

PCanvas com.google.scrollview.ui.SVWindow.canvas

Definition at line 113 of file SVWindow.java.

int com.google.scrollview.ui.SVWindow.hash

A unique representation for the window, also known by the client. It is used when sending messages from server to client to identify him.

Definition at line 97 of file SVWindow.java.

int com.google.scrollview.ui.SVWindow.nrWindows = 0
static

The total number of created Windows. If this ever reaches 0 (apart from the beginning), quit the server.

Definition at line 103 of file SVWindow.java.

final double com.google.scrollview.ui.SVWindow.SCALING_FACTOR = 2
static

Constant defining the "speed" at which to zoom in and out.

Definition at line 63 of file SVWindow.java.

SVPopupMenu com.google.scrollview.ui.SVWindow.svPuMenu = null

Definition at line 112 of file SVWindow.java.


The documentation for this class was generated from the following file: