KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > text > WrappedDisplay


1 package text;
2
3 import text.wrapped.*;
4
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.util.*;
8 import javax.swing.*;
9 import javax.swing.event.*;
10 import java.awt.font.*;
11 import java.awt.datatransfer.*;
12
13 import rero.config.*;
14 import rero.gui.script.WindowAreaClickListener;
15 import rero.gui.windows.ChannelWindow;
16 import rero.gui.SessionManager;
17
18 import text.event.*;
19
20 public class WrappedDisplay extends JComponent implements MouseWheelListener, MouseInputListener, ClientStateListener
21 {
22    protected WrappedDisplayComponent display;
23    protected JScrollBar scroller;
24    protected WrappedData data;
25    protected FindDialog search = null;
26
27    public void showSearchDialog(String JavaDoc title)
28    {
29       if (search == null)
30       {
31          search = new FindDialog(this, title, this, "");
32       }
33
34       search.showDialog();
35    }
36
37    public void addText(String JavaDoc text)
38    {
39       data.addText(new WrappedContainer(text));
40    }
41
42    public void addTextTable(String JavaDoc single, String JavaDoc text[], double percentage)
43    {
44       data.addText(new WrappedNames(single, text, percentage));
45    }
46
47    public void propertyChanged(String JavaDoc a, String JavaDoc b)
48    {
49       data.dirty(); // invalidates all stored text
50
}
51
52    public void clear()
53    {
54       data.reset();
55    }
56
57    /** returns a linked list of integers indicating which lines the text was found at */
58    public ListIterator find(String JavaDoc text)
59    {
60       return data.find(text);
61    }
62
63    public void scroll(int scrollSize)
64    {
65       data.setValue(data.getValue() + scrollSize);
66       repaint();
67    }
68
69    public void scrollTo(int specifics)
70    {
71       data.setValue(specifics);
72       repaint();
73    }
74
75    public void mouseWheelMoved(MouseWheelEvent e)
76    {
77       int scrollSize = e.getScrollAmount() - 1;
78       if (scrollSize <= 0) { scrollSize = 1; }
79
80       if (e.getWheelRotation() >= 0) // up
81
{
82           data.setValue(data.getValue() + scrollSize);
83           repaint();
84       }
85       else // down
86
{
87           data.setValue(data.getValue() - scrollSize);
88           repaint();
89       }
90    }
91
92    public WrappedDisplay()
93    {
94       scroller = new JScrollBar(JScrollBar.VERTICAL, 0, 0, 0, 0);
95       display = new WrappedDisplayComponent();
96
97       data = new WrappedData();
98
99       display.setWrappedData(data);
100       scroller.setModel(data);
101
102       addMouseWheelListener(this);
103       addMouseListener(this);
104       addMouseMotionListener(this);
105  
106       data.addChangeListener(display);
107
108       setLayout(new BorderLayout());
109       add(scroller, BorderLayout.EAST);
110       add(display, BorderLayout.CENTER);
111
112       setOpaque(false);
113       setDoubleBuffered(false);
114
115 // setBorder(null);
116
setBorder(BorderFactory.createEmptyBorder(0, TextSource.UNIVERSAL_TWEAK, 0, 0));
117
118       ClientState.getClientState().addClientStateListener("ui.font", this);
119    }
120
121    public void mousePressed(MouseEvent ev)
122    {
123       if (ev.isPopupTrigger()) { data.setValueIsAdjusting(false); return; }
124
125       data.setValueIsAdjusting(true);
126       data.setSelection(new SelectionSpace(ev.getPoint()));
127       repaint();
128    }
129
130    public void mouseReleased(MouseEvent ev)
131    {
132       data.setValueIsAdjusting(false);
133
134       if (ev.isPopupTrigger()) { return; }
135
136       if (data.getSelection() != null && data.getSelection().getSelectedText() != null && data.getSelection().getSelectedText().length() > 0)
137       {
138          StringSelection selected = new StringSelection(data.getSelection().getSelectedText());
139
140          Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selected, selected);
141
142          if (Toolkit.getDefaultToolkit().getSystemSelection() != null)
143          {
144             Toolkit.getDefaultToolkit().getSystemSelection().setContents(selected, selected);
145          }
146       }
147
148       data.setSelection(null);
149       repaint();
150    }
151
152    public void mouseClicked(MouseEvent ev)
153    {
154       if (ev.isPopupTrigger()) { data.setValueIsAdjusting(false); return; }
155
156       if (ev.isShiftDown())
157       {
158          WrappedObject temp = data.getAttributesAt(display.getHeight(), ev.getX(), ev.getY());
159
160          if (temp != null && temp.getObject() != null)
161          {
162             AttributedText attribs = (AttributedText)(temp.getObject());
163             int index;
164      
165             if (ev.isControlDown() && attribs.backIndex > -1)
166             {
167                index = attribs.backIndex;
168             }
169             else
170             {
171                index = attribs.foreIndex;
172             }
173             
174             ModifyColorMapDialog.showModifyColorMapDialog(this, index);
175          }
176          return;
177       }
178
179       WrappedObject temp = data.getTokenAt(display.getHeight(), ev.getX(), ev.getY());
180
181       if (temp != null && temp.getObject() != null)
182       {
183          if (fireClickEvent(temp.getObject().toString(), temp.getText(), ev).isAcknowledged())
184          {
185             setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
186
187             new Thread JavaDoc(new Runnable JavaDoc()
188             {
189                public void run()
190                {
191                   try {
192                   Thread.sleep(300);
193                   setCursor(Cursor.getDefaultCursor()); } catch (Exception JavaDoc zex) { zex.printStackTrace(); }
194                }
195             }).start();
196          }
197       } else {
198         String JavaDoc wname = SessionManager.getGlobalCapabilities().getActiveSession().getActiveWindow().getName();
199         fireClickEvent(null, wname, ev);
200       }
201    }
202
203    public void mouseEntered(MouseEvent ev)
204    {
205       // we have nothing to do for this event...
206
}
207
208    public void mouseExited(MouseEvent ev)
209    {
210       // nothing to do for this event...
211
}
212
213    public void mouseDragged(MouseEvent ev)
214    {
215       if (ev.isPopupTrigger()) { return; }
216
217       if (data.getSelection() != null)
218       {
219          data.getSelection().growSelection(ev.getPoint());
220          Rectangle temp = data.getSelection().getChangedArea();
221          repaint(temp);
222       }
223    }
224
225    public void mouseMoved(MouseEvent ev)
226    {
227       // again nothing to do for this event...
228
}
229
230    protected LinkedList listeners = new LinkedList();
231
232    public void addClickListener(ClickListener l)
233    {
234       listeners.add(l);
235    }
236
237    public ClickEvent fireClickEvent(String JavaDoc text, String JavaDoc context, MouseEvent ev)
238    {
239       ClickEvent event = new ClickEvent(text, context, ev);
240
241       ListIterator i = listeners.listIterator();
242       while (i.hasNext() && !event.isConsumed())
243       {
244          ClickListener l = (ClickListener)i.next();
245         // pass events with null text only to area click listener
246
if(text == null && !(l instanceof WindowAreaClickListener)) continue;
247         l.wordClicked(event);
248       }
249
250       return event;
251    }
252
253 /* protected void finalize()
254    {
255       System.out.println("Finalizing the wrapped display");
256    } */

257 }
258
259
260
261
Popular Tags