Archive for September, 2007

Java Swing O Reilly // (Free php web host) Save the state

Wednesday, September 26th, 2007

Java Swing O Reilly // Save the state of the app by storing the current state of the threebuttons. // We’ll use the buttons themselves as keys and their selected state asvalues. public void storeState(Hashtable ht) { ht.put(tog, new Boolean(tog.isSelected())); ht.put(cb, new Boolean(cb.isSelected())); ht.put(radio, new Boolean(radio.isSelected())); } // Restore state based on the values we saved when storeState() was called. // Note that StateEdit discards any state info that did not change frombetween // the start state and the end state, so we can’t assume that the state for all // 3 buttons is in the Hashtable. public void restoreState(Hashtable ht) { Boolean b1 = (Boolean)ht.get(tog); if (b1 != null) tog.setSelected(b1.booleanValue()); Boolean b2 = (Boolean)ht.get(cb); if (b2 != null) cb.setSelected(b2.booleanValue()); Boolean b3 = (Boolean)ht.get(radio); if (b3 != null) radio.setSelected(b3.booleanValue()); } private JToggleButton tog; private JCheckBox cb; private JRadioButton radio; private JButton undoButton; private JButton redoButton; private JButton startButton; private JButton endButton; private StateEdit edit; // Main program just creates the frame and displays it. public static void main(String[] args) { JFrame f = new UndoableToggleApp4(); f.addWindowListener(new BasicWindowMonitor()); f.setVisible(true); } } Note that we could have used whatever keys and values we needed to store the current state in the storeState() method. In this example, an easy strategy was to use the button itself as the key and a Boolean to hold the value. There are no restrictions on the keys and values you choose, as long as they are Objects and the storeState() and restoreState() methods are implemented to use the same keys. - 570 -
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

Java Swing O Reilly edit.end(); (Web hosting e commerce) startButton.setEnabled(true); endButton.setEnabled(false); undoButton.setEnabled(edit.canUndo());

Tuesday, September 25th, 2007

Java Swing O Reilly edit.end(); startButton.setEnabled(true); endButton.setEnabled(false); undoButton.setEnabled(edit.canUndo()); redoButton.setEnabled(edit.canRedo()); } }); // Add a listener to the undo button. It attempts to call undo() on the// current edit, then enables/disables the undo/redo buttons as appropriate. undoButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { try { edit.undo(); } catch (CannotUndoException ex) { ex.printStackTrace(); } finally { undoButton.setEnabled(edit.canUndo()); redoButton.setEnabled(edit.canRedo()); } } }); // Add a redo listener: just like the undo listener. redoButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { try { edit.redo(); } catch (CannotRedoException ex) { ex.printStackTrace(); } finally { undoButton.setEnabled(edit.canUndo()); redoButton.setEnabled(edit.canRedo()); } } }); // Lay out the state/end and undo/redo buttons Box undoRedoBox = new Box(BoxLayout.X_AXIS); undoRedoBox.add(Box.createGlue()); undoRedoBox.add(startButton); undoRedoBox.add(Box.createHorizontalStrut(2)); undoRedoBox.add(endButton); undoRedoBox.add(Box.createHorizontalStrut(2)); undoRedoBox.add(undoButton); undoRedoBox.add(Box.createHorizontalStrut(2)); undoRedoBox.add(redoButton); undoRedoBox.add(Box.createGlue()); // Lay out the main frame Container content = getContentPane(); content.setLayout(new BorderLayout()); content.add(buttonBox, BorderLayout.CENTER); content.add(undoRedoBox, BorderLayout.SOUTH); setSize(400, 150); } // When any toggle button is clicked, we turn off the undo and redo buttons, // reflecting the fact that we can only undo/redo the last set of state changes// as long as no additional changes have been made. public void actionPerformed(ActionEvent ev) { undoButton.setEnabled(false); redoButton.setEnabled(false); } - 569 -
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

Web site developers - Java Swing O Reilly 18.1.8.1 State Optimization It’s

Wednesday, September 19th, 2007

Java Swing O Reilly 18.1.8.1 State Optimization It’s important to understand that StateEdit optimizes its representation of the states returned by the StateEditable. This is done by removing all duplicate key/value pairs from the pre and post Hashtables, keeping only those values that have changed. This is important to understand because it means the StateEditable cannot assume that all keys and values stored by its storeState() method will be in the table that gets passed into restoreState(). Figure 18.10 shows an example of how this works. The top two tables show the complete state as returned by the StateEditable. The bottom two tables show the tables as they appear after StateEdit.end() has compressed them to remove duplicate data. This is how the tables would look when passed to State-Editable.restoreState(). Figure 18.10. Example of state compression done by StateEdit 18.1.8.2 Property - 565 -
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Java Swing O Reilly 18.1.7 The StateEditable Interface

Tuesday, September 18th, 2007

Java Swing O Reilly 18.1.7 The StateEditable Interface So far in this chapter, we’ve seen that the responsibility for undoing or redoing an edit lies in the UndoableEdit object itself. The Swing undo package also provides another mechanism for handling undo and redo, which is based on the idea of letting an “outside object” define its state before and after a series of changes are made to it. Once these pre and post states of the object have been defined, a StateEdit can be used to toggle back and forth between these states, undoing and redoing the changes. The “outside object” is responsible for defining the object’s significant state, and must implement the StateEditable interface, which defines the methods listed below. 18.1.7.1 Methods StateEditable defines two simple methods: public void storeState(Hashtable state) This method is called to ask the object to store its current state by inserting attributes and values as key/value pairs into the given Hashtable. public void restoreState(Hashtable state) This method is called to tell the object to restore its state, based on the key/value pairs found in the given Hashtable. 18.1.8 The StateEdit Class StateEdit is an extension of AbstractUndoableEdit that is used to toggle between two arbitrary states. These states are defined by a StateEditable associated with the StateEdit. When the edit is created, it gets the current (pre) state from the StateEditable. Later, when end() is called on the edit (presumably after some changes have been made to the state of the StateEditable), it again gets the current (post) state from the StateEditable. After this point, undo() and redo() calls result in the state of the StateEditable being toggled back and forth between the pre and post states. Figure 18.9 shows a typical sequence of method calls between an application object (some object in the system that is managing edits), a StateEdit, its StateEditable, and the two Hashtables used to store the state of the StateEditable. Figure 18.9. StateEdit sequence diagram - 564 -
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

Java Swing O Reilly Insignificant edit example Undo (Virtual web hosting)

Tuesday, September 18th, 2007

Java Swing O Reilly Insignificant edit example Undo 4 // 3 undos from a single mgr.undo() call . . . Undo 3 Undo 2 Redo 2 // but mgr.redo() only redoes the significant one! false // . . . and there are no more redos Absorbed (by addEdit) edit example Undo 5 (absorbed: <6>) // edit 6 was absorbed by edit 5 and undone Absorbed (by replaceEdit) edit example Undo 2 (replaced: <1>) // edit 1 was replaced by edit 2 and undone false // no more edits to undo Changing limit example Undo 6 // we do three undos . . . Undo 5 Undo 4 // . . . and then set the limit to 4 which trims from both ends Undo 3 // only 2 undos left . . . Undo 2 Redo 2 // and then 4 redos are available . .. Redo 3 Redo 4 Redo 5 undoOrRedo example Undo // undoOrRedoPresentationName is “Undo” here… Undo 1 // …then we do an undoOrRedo()… Redo // …and it’s now “Redo” Redo 1 Transform to composite example Undo 3 // because we called end(), undo() undoes all the edits . . . Undo 2 Undo 1 Redo 1 // . . . and redo() redoes them all . . . Redo 2 Redo 3 true // addEdit() claims the edit was added. . . (returns false in JDK 1.2) Undo 3 // but edit 4 never got added because end() had been called . . . Undo 2 Undo 1 All of the details shown in this example can be a little overwhelming, but don’t let this keep you from using UndoManager. For most applications, the basic features of UndoManager (shown in the first example in this section) will give you everything you need to provide powerful undo capabilities to your users. 18.1.6.12 Extending UndoManager At the end of this chapter, we’ll show how you might extend UndoManager to add additional functionality. We’ll create an undo manager that gives us access to the edits it contains and notifies us any time an edit is added. - 563 -
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Java Swing O Reilly // Show how changing (Web site builder)

Monday, September 17th, 2007

Java Swing O Reilly // Show how changing limit works mgr.discardAllEdits(); // # adds? sig? replace? mgr.addEdit(new SampleUndoableEdit(1, false, true, false)); mgr.addEdit(new SampleUndoableEdit(2, false, true, false)); mgr.addEdit(new SampleUndoableEdit(3, false, true, false)); mgr.addEdit(new SampleUndoableEdit(4, false, true, false)); mgr.addEdit(new SampleUndoableEdit(5, false, true, false)); mgr.addEdit(new SampleUndoableEdit(6, false, true, false)); System.out.println(”———————-”); System.out.println(”Changing limit example”); System.out.println(”———————-”); mgr.undo(); mgr.undo(); mgr.undo(); // now 3 undoable, 3 redoable mgr.setLimit(4); // now 2 undoable, 2 redoable! while (mgr.canUndo()) mgr.undo(); while (mgr.canRedo()) mgr.redo(); // undoOrRedo example mgr.discardAllEdits(); mgr.setLimit(1); // # adds? sig? replace? mgr.addEdit(new SampleUndoableEdit(1, false, true, false)); System.out.println(”——————”); System.out.println(”undoOrRedo example”); System.out.println(”——————”); System.out.println(mgr.getUndoOrRedoPresentationName()); mgr.undoOrRedo(); System.out.println(mgr.getUndoOrRedoPresentationName()); mgr.undoOrRedo(); // Show how UndoManager becomes a CompositeEdit mgr.discardAllEdits(); mgr.setLimit(100); // # adds? sig? replace? mgr.addEdit(new SampleUndoableEdit(1, false, true, false)); mgr.addEdit(new SampleUndoableEdit(2, false, true, false)); mgr.addEdit(new SampleUndoableEdit(3, false, true, false)); System.out.println(”——————————”); System.out.println(”Transform to composite example”); System.out.println(”——————————”); mgr.end(); mgr.undo(); mgr.redo(); // Show that adds are no longer allowed. Note that addEdit() returns true in// pre-JDK 1.2 Swing releases. This is fixed in JDK 1.2. System.out.println(mgr.addEdit( new SampleUndoableEdit(4, false, true, false))); mgr.undo(); // note that edit 4 is not there} } Here’s the output generated by this program. We’ve added some comments to the output in italic. - 562 -
Check Tomcat Web Hosting services for best quality webspace to host your web application.

Java Swing O Reilly System.out.println(); } private boolean (Most popular web site)

Monday, September 17th, 2007

Java Swing O Reilly System.out.println(); } private boolean isSignificant; private boolean isReplacer; private int number; private boolean allowAdds; private Vector addedEdits; private UndoableEdit replaced; } In our main program, we’ll add instances of this new edit class to an UndoManager to show how different features work. We won’t step through this program line-by-line. The comments in the code and in the output should serve as an explanation of the different UndoManager features (and quirks) being shown:. // UndoManagerDetails.java // import javax.swing.undo.*; // An example that shows lots of little UndoManager details. public class UndoManagerDetails { public static void main(String[] args) { UndoManager mgr = new UndoManager(); // Show how insignificant edits are skipped over // // # adds? sig? replace? mgr.addEdit(new SampleUndoableEdit(1, false, true, false)); mgr.addEdit(new SampleUndoableEdit(2, false, true, false)); mgr.addEdit(new SampleUndoableEdit(3, false, false, false)); mgr.addEdit(new SampleUndoableEdit(4, false, false, false)); System.out.println(”————————–”); System.out.println(”Insignificant edit example”); System.out.println(”————————–”); mgr.undo(); mgr.redo(); System.out.println(mgr.canRedo()); // no more sig. edits // Show how edits which call add/replace are used // // # adds? sig? replace? mgr.addEdit(new SampleUndoableEdit(5, true, true, false)); mgr.addEdit(new SampleUndoableEdit(6, false, true, false)); System.out.println(”———————————-”); System.out.println(”Absorbed (by addEdit) edit example”); System.out.println(”———————————-”); mgr.undo(); mgr.discardAllEdits(); // # adds? sig? replace? mgr.addEdit(new SampleUndoableEdit(1, false, true, false)); mgr.addEdit(new SampleUndoableEdit(2, false, true, true)); System.out.println(”————————————–”); System.out.println(”Absorbed (by replaceEdit) edit example”); System.out.println(”————————————–”); mgr.undo(); System.out.println(mgr.canUndo()); - 561 -
From our experience, we can recommend PHP5 Web Hosting services, if you need affordable webhost to host and run your web application.

Java Swing O Reilly this.allowAdds = allowAdds; if (Web file server)

Sunday, September 16th, 2007

Java Swing O Reilly this.allowAdds = allowAdds; if (allowAdds) addedEdits = new Vector(); this.isSignificant = isSignificant; this.isReplacer = isReplacer; } // “Undo” the edit by printing a message to the screen. public void undo() throws CannotUndoException { super.undo(); System.out.print(”Undo ” + number); dumpState(); } // “Redo” the edit by printing a message to the screen. public void redo() throws CannotRedoException { super.redo(); System.out.print(”Redo ” + number); dumpState(); } // If allowAdds is true, we store the input edit. If not, just return false. public boolean addEdit(UndoableEdit anEdit) { if (allowAdds) { addedEdits.addElement(anEdit); return true; } else return false; } // If isReplacer is true, we store the edit we are replacing. public boolean replaceEdit(UndoableEdit anEdit) { if (isReplacer) { replaced = anEdit; return true; } else return false; } // Significance is based on constructor parameter. public boolean isSignificant() { return isSignificant; } // Just return our identifier. public String toString() { return “<" + number + ">“; } // Debug output. public void dumpState() { if (allowAdds && addedEdits.size() > 0) { Enumeration e = addedEdits.elements(); System.out.print(” (absorbed: “); while (e.hasMoreElements()) { System.out.print(e.nextElement()); } System.out.print(”)”); } if (isReplacer && replaced != null) { System.out.print(” (replaced: ” + replaced + “)”); } - 560 -
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Web site designers - Java Swing O Reilly // Method to set

Sunday, September 16th, 2007

Java Swing O Reilly // Method to set the text and state of the undo/redo buttons. protected void updateButtons() { undoButton.setText(manager.getUndoPresentationName()); redoButton.setText(manager.getRedoPresentationName()); undoButton.getParent().validate(); undoButton.setEnabled(manager.canUndo()); redoButton.setEnabled(manager.canRedo()); } private UndoManager manager = new UndoManager(); private JButton undoButton; private JButton redoButton; // Main program just creates the frame and displays it. public static void main(String[] args) { JFrame f = new UndoableToggleApp3(); f.addWindowListener(new BasicWindowMonitor()); f.setVisible(true); } } Figure 18.8 shows the application running. Before taking this screen shot, we toggled each of the buttons in order and then undid the third toggle. Notice that we can now re-toggle button three or undo the previous toggle (button two). Figure 18.8. Undoing and redoing toggle buttons 18.1.6.11 Understanding the UndoManager in-depth There are a lot of subtle details about UndoManager that may be hard to understand without seeing them in action. In this section, we’ll try to provide a concrete example of how all these little things work. To do so, let’s create a very simple UndoableEdit implementation, not actually associated with any component. It will serve to help us see what the UndoManager is doing in certain situations. All it does is output various bits of useful information when its methods are called. Here’s the code for this class: // SampleUndoableEdit.java // import javax.swing.undo.*; import java.util.*; public class SampleUndoableEdit extends AbstractUndoableEdit { // Create a new edit with an identifying number. The boolean arguments define // the edit’s behavior. public SampleUndoableEdit(int number, boolean allowAdds, boolean isSignificant, boolean isReplacer) { this.number = number; - 559 -
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.

Java Swing O Reilly (Web hosting comparison) tog2.addUndoableEditListener(this); tog3.addUndoableEditListener(this); // Lay

Saturday, September 15th, 2007

Java Swing O Reilly tog2.addUndoableEditListener(this); tog3.addUndoableEditListener(this); // Lay out the buttons Box buttonBox = new Box(BoxLayout.Y_AXIS); buttonBox.add(tog1); buttonBox.add(tog2); buttonBox.add(tog3); // Create undo and redo buttons (initially disabled) undoButton = new JButton(”Undo”); redoButton = new JButton(”Redo”); undoButton.setEnabled(false); redoButton.setEnabled(false); // Add a listener to the undo button. It attempts to call undo() on the// UndoManager, then enables/disables the undo/redo buttons as appropriate. undoButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { try { manager.undo(); } catch (CannotUndoException ex) { ex.printStackTrace(); } finally { updateButtons(); } } }); // Add a redo listener: just like the undo listener. redoButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { try { manager.redo(); } catch (CannotRedoException ex) { ex.printStackTrace(); } finally { updateButtons(); } } }); // Lay out the undo/redo buttons Box undoRedoBox = new Box(BoxLayout.X_AXIS); undoRedoBox.add(Box.createGlue()); undoRedoBox.add(undoButton); undoRedoBox.add(Box.createHorizontalStrut(2)); undoRedoBox.add(redoButton); undoRedoBox.add(Box.createGlue()); // Lay out the main frame getContentPane().setLayout(new BorderLayout()); getContentPane().add(buttonBox, BorderLayout.CENTER); getContentPane().add(undoRedoBox, BorderLayout.SOUTH); setSize(400, 150); } // When an UndoableEditEvent is generated (each time one of the buttons is// pressed), we add it to the UndoManager and then get the manager’s undo/redo// names and set the undo/redo button labels. Finally, we enable/disable these// buttons by asking the manager what we are allowed to do. public void undoableEditHappened(UndoableEditEvent ev) { manager.addEdit(ev.getEdit()); updateButtons(); } - 558 -
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.