- Java view class example
- Field Summary
- Fields inherited from interface javax.swing.SwingConstants
- Constructor Summary
- Method Summary
- Methods inherited from class java.lang.Object
- Field Detail
- BadBreakWeight
- GoodBreakWeight
- ExcellentBreakWeight
- ForcedBreakWeight
- X_AXIS
- Y_AXIS
- Java View tutorial with examples
- Introduction
- Example
- Related
Java view class example
A very important part of the text package is the View class. As the name suggests it represents a view of the text model, or a piece of the text model. It is this class that is responsible for the look of the text component. The view is not intended to be some completely new thing that one must learn, but rather is much like a lightweight component. By default, a view is very light. It contains a reference to the parent view from which it can fetch many things without holding state, and it contains a reference to a portion of the model ( Element ). A view does not have to exactly represent an element in the model, that is simply a typical and therefore convenient mapping. A view can alternatively maintain a couple of Position objects to maintain its location in the model (i.e. represent a fragment of an element). This is typically the result of formatting where views have been broken down into pieces. The convenience of a substantial relationship to the element makes it easier to build factories to produce the views, and makes it easier to keep track of the view pieces as the model is changed and the view must be changed to reflect the model. Simple views therefore represent an Element directly and complex views do not. A view has the following responsibilities: Participate in layout. The view has a setSize method which is like doLayout and setSize in Component combined. The view has a preferenceChanged method which is like invalidate in Component except that one can invalidate just one axis and the child requesting the change is identified. A View expresses the size that it would like to be in terms of three values, a minimum, a preferred, and a maximum span. Layout in a view is can be done independently upon each axis. For a properly functioning View implementation, the minimum span will be The minimum set of methods for layout are:
- getMinimumSpan
- getPreferredSpan
- getMaximumSpan
- getAlignment
- preferenceChanged
- setSize
The setSize method should be prepared to be called a number of times (i.e. It may be called even if the size didn’t change). The setSize method is generally called to make sure the View layout is complete prior to trying to perform an operation on it that requires an up-to-date layout. A view’s size should always be set to a value within the minimum and maximum span specified by that view. Additionally, the view must always call the preferenceChanged method on the parent if it has changed the values for the layout it would like, and expects the parent to honor. The parent View is not required to recognize a change until the preferenceChanged has been sent. This allows parent View implementations to cache the child requirements if desired. The calling sequence looks something like the following: The exact calling sequence is up to the layout functionality of the parent view (if the view has any children). The view may collect the preferences of the children prior to determining what it will give each child, or it might iteratively update the children one at a time. Render a portion of the model. This is done in the paint method, which is pretty much like a component paint method. Views are expected to potentially populate a fairly large tree. A View has the following semantics for rendering:
- The view gets its allocation from the parent at paint time, so it must be prepared to redo layout if the allocated area is different from what it is prepared to deal with.
- The coordinate system is the same as the hosting Component (i.e. the Component returned by the getContainer method). This means a child view lives in the same coordinate system as the parent view unless the parent has explicitly changed the coordinate system. To schedule itself to be repainted a view can call repaint on the hosting Component .
- The default is to not clip the children. It is more efficient to allow a view to clip only if it really feels it needs clipping.
- The Graphics object given is not initialized in any way. A view should set any settings needed.
- A View is inherently transparent. While a view may render into its entire allocation, typically a view does not. Rendering is performed by traversing down the tree of View implementations. Each View is responsible for rendering its children. This behavior is depended upon for thread safety. While view implementations do not necessarily have to be implemented with thread safety in mind, other view implementations that do make use of concurrency can depend upon a tree traversal to guarantee thread safety.
- The order of views relative to the model is up to the implementation. Although child views will typically be arranged in the same order that they occur in the model, they may be visually arranged in an entirely different order. View implementations may have Z-Order associated with them if the children are overlapping.
The methods for rendering are:
- paint
Translate between the model and view coordinate systems. Because the view objects are produced from a factory and therefore cannot necessarily be counted upon to be in a particular pattern, one must be able to perform translation to properly locate spatial representation of the model. The methods for doing this are:
- modelToView
- viewToModel
- getDocument
- getElement
- getStartOffset
- getEndOffset
The layout must be valid prior to attempting to make the translation. The translation is not valid, and must not be attempted while changes are being broadcasted from the model via a DocumentEvent . Respond to changes from the model. If the overall view is represented by many pieces (which is the best situation if one want to be able to change the view and write the least amount of new code), it would be impractical to have a huge number of DocumentListener s. If each view listened to the model, only a few would actually be interested in the changes broadcasted at any given time. Since the model has no knowledge of views, it has no way to filter the broadcast of change information. The view hierarchy itself is instead responsible for propagating the change information. At any level in the view hierarchy, that view knows enough about its children to best distribute the change information further. Changes are therefore broadcasted starting from the root of the view hierarchy. The methods for doing this are:
- insertUpdate
- removeUpdate
- changedUpdate
Field Summary
The weight to indicate a view supports breaking, and this represents a very attractive place to break.
The weight to indicate a view supports breaking, and must be broken to be represented properly when placed in a view that formats its children by breaking them.
Fields inherited from interface javax.swing.SwingConstants
Constructor Summary
Method Summary
Gives notification from the document that attributes were changed in a location that this view is responsible for.
Forwards the given DocumentEvent to the child views that need to be notified of the change to the model.
Provides a way to determine the next visually represented model location at which one might place a caret.
Gives notification that something was inserted into the document in a location that this view is responsible for.
Provides a mapping, for a given region, from the document model coordinate space to the view coordinate space.
Provides a mapping, for a given character, from the document model coordinate space to the view coordinate space.
Child views can call this on the parent to indicate that the preference has changed and should be reconsidered for layout.
Gives notification that something was removed from the document in a location that this view is responsible for.
Updates the child views in response to receiving notification that the model changed, and there is change record for the element this view is responsible for.
Methods inherited from class java.lang.Object
Field Detail
BadBreakWeight
public static final int BadBreakWeight
The weight to indicate a view is a bad break opportunity for the purpose of formatting. This value indicates that no attempt should be made to break the view into fragments as the view has not been written to support fragmenting.
GoodBreakWeight
public static final int GoodBreakWeight
ExcellentBreakWeight
public static final int ExcellentBreakWeight
The weight to indicate a view supports breaking, and this represents a very attractive place to break.
ForcedBreakWeight
public static final int ForcedBreakWeight
The weight to indicate a view supports breaking, and must be broken to be represented properly when placed in a view that formats its children by breaking them.
X_AXIS
public static final int X_AXIS
Y_AXIS
public static final int Y_AXIS
Java View tutorial with examples
A very important part of the text package is the View class.
Introduction
A very important part of the text package is the View class.
As the name suggests it represents a view of the text model, or a piece of the text model.
It is this class that is responsible for the look of the text component.
The view is not intended to be some completely new thing that one must learn, but rather is much like a lightweight component.
By default, a view is very light.
It contains a reference to the parent view from which it can fetch many things without holding state, and it contains a reference to a portion of the model (Element).
A view does not have to exactly represent an element in the model, that is simply a typical and therefore convenient mapping.
Example
The following code shows how to use View from javax.swing.text.
import javax.swing.JTextPane; import javax.swing.text.JTextComponent; import javax.swing.text.View; public class Main < public static void main(String[] argv) < JTextComponent textComp = new JTextPane(); View v = textComp.getUI().getRootView(textComp); walkView(v, 0); > public static void walkView(View view, int level) int n = view.getViewCount(); for (int i = 0; i < n; i++) < walkView(view.getView(i), level + 1); >> >
import javax.swing.UIManager; import javax.swing.text.*; import javax.swing.plaf.*; import java.io.*; public class TextComponentDisplay < public static void displayModel(JTextComponent comp, PrintStream out) < Document doc = comp.getDocument(); if (doc instanceof AbstractDocument) < ((AbstractDocument) doc).dump(out); >// w w w . de m o 2 s .c om > public static void displayViews(JTextComponent comp, PrintStream out) < TextUI textUI = (TextUI) comp.getUI(); View rootView = textUI.getRootView(comp); displayView(rootView, 0, out); > public static void displayView(View view, int tabs, PrintStream out) // Print info about this view for (int i = 0; i < tabs; i++) < out.print("\t"); > out.println(view.getClass().getName()); for (int i = 0; i < tabs; i++) < out.print("\t"); > out.println("Start: " + view.getStartOffset() + "; end: " + view.getEndOffset()); // Display child views, if any. int childViews = view.getViewCount(); for (int i = 0; i < childViews; i++) < View childView = view.getView(i); displayView(childView, tabs + 1, out); > > >
import java.io.PrintStream; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.text.BadLocationException; import javax.swing.text.Document; import javax.swing.text.JTextComponent; import javax.swing.text.View; public class TextFieldViews < public static void main(String[] args) < try /* ww w . de m o 2 s . c o m*/ UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); > catch (Exception evt) < > JFrame f = new JFrame("Text Field View"); JTextField tf = new JTextField(32); tf.setText("That's one small step for man. "); f.getContentPane().add(tf); f.pack(); f.setVisible(true); ViewDisplayer.displayViews(tf, System.out); > > class ViewDisplayer < public static void displayViews(JTextComponent comp, PrintStream out) < View rootView = comp.getUI().getRootView(comp); displayView(rootView, 0, comp.getDocument(), out); > public static void displayView(View view, int indent, Document doc, PrintStream out) String name = view.getClass().getName(); for (int i = 0; i < indent; i++) < out.print("\t"); > int start = view.getStartOffset(); int end = view.getEndOffset(); out.println(name + "; offsets [" + start + ", " + end + "]"); int viewCount = view.getViewCount(); if (viewCount == 0) < int length = Math.min(32, end - start); try < String txt = doc.getText(start, length); for (int i = 0; i < indent + 1; i++) < out.print("\t"); > out.println("[" + txt + "]"); > catch (BadLocationException e) < >> else < for (int i = 0; i < viewCount; i++) < displayView(view.getView(i), indent + 1, doc, out); >> > >
Related
demo2s.com | Email: | Demo Source and Support. All rights reserved.