KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > jemacs > buffer > EWindow


1 // Copyright (c) 2002 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.jemacs.buffer;
5 import gnu.lists.*;
6 import gnu.mapping.*;
7
8 public abstract class EWindow
9 {
10   public EFrame frame;
11   public Buffer buffer;
12
13   protected int[] pendingKeys = null;
14   protected int pendingLength = 0;
15
16  /** Previous window in cyclic window ordering. */
17   protected EWindow prevWindow;
18
19   /** Next window in cyclic window ordering. */
20   protected EWindow nextWindow;
21   
22   /** Nominal height in pixels of a character, if non-zero. */
23   protected int charHeight;
24
25   /** Nominal width in pixels of a character, if non-zero. */
26   protected int charWidth;
27
28   public EWindow (Buffer buffer)
29   {
30     this.buffer = buffer;
31     this.nextWindow = this;
32     this.prevWindow = this;
33   }
34
35   public static EWindow getSelected()
36   {
37     return EFrame.selectedFrame == null ? null
38       : EFrame.selectedFrame.selectedWindow;
39   }
40
41   public void setSelected()
42   {
43     EWindow selected = getSelected();
44     if (selected != null && selected.buffer != buffer)
45       selected.unselect();
46
47     if (frame != null)
48       frame.selectedWindow = this;
49     EFrame.selectedFrame = frame;
50     Buffer.setCurrent(buffer);
51
52   }
53
54   public abstract void unselect();
55   
56   public static void setSelected(EWindow window)
57   {
58     window.setSelected();
59     window.requestFocus();
60   }
61
62   public void requestFocus()
63   {
64   }
65
66   public EFrame getFrame()
67   {
68     return frame;
69   }
70
71   public final void setFrame(EFrame frame) { this.frame = frame; }
72
73   public Buffer getBuffer()
74   {
75     return buffer;
76   }
77
78   public void setBuffer (Buffer buffer)
79   {
80     this.buffer = buffer;
81   }
82
83   /** Returns the "Emacs value" (1-origin) of point. */
84   public abstract int getPoint();
85
86   public final void setPoint(int point)
87   {
88     setDot(point - 1);
89   }
90
91   public abstract void setDot(int offset);
92
93   /** Split this window into two, showing this buffer in both windows.
94    * @return the new wndow.
95    */

96   public final EWindow split (int lines, boolean horizontal)
97   {
98     return split(buffer, lines, horizontal);
99   }
100
101   /** Split this window into two.
102    * Display Var>buffer</var> in the new window.
103    * @return the new window.
104    */

105   public abstract EWindow split (Buffer buffer, int lines, boolean horizontal);
106
107   /** Link a new window after this. */
108   protected final void linkSibling (EWindow window, boolean horizontal)
109   {
110     this.nextWindow = window;
111     window.prevWindow = this;
112     EWindow next = nextWindow;
113     window.nextWindow = next;
114     // next is non-null, since the order is cyclic.
115
next.prevWindow = window;
116   }
117
118   protected final void unlink()
119   {
120     if (frame.firstWindow == this)
121       {
122     if (nextWindow == this)
123       frame.firstWindow = null;
124     else
125       frame.firstWindow = nextWindow;
126       }
127     nextWindow.prevWindow = prevWindow;
128     prevWindow.nextWindow = nextWindow;
129     prevWindow = this;
130     nextWindow = this;
131   }
132
133   /** Return the next/previous window in the cyclic order of windows.
134    * Returns null if this is the last/first window in this EFrame. */

135   public EWindow getNextWindow(boolean forwards)
136   {
137     return nextWindow;
138   }
139
140   /** Return the next/previous EWindow in the cyclic order of windows.
141    * Returns first/last if this is the last/first window in this EFrame. */

142   public final EWindow getOtherWindow(boolean forwards)
143   {
144     EWindow win = getNextWindow(forwards); // FIXME
145
/*
146     if (win == null)
147       win = forwards ? frame.getFirstWindow() : frame.getLastWindow();
148     */

149     return win;
150   }
151
152   public final EWindow getNextWindowInFrame(int count)
153   {
154     EWindow win = this;
155     if (count > 0)
156       {
157         while (--count >= 0)
158           win = win.getOtherWindow(true);
159       }
160     else
161       {
162         while (++count <= 0)
163           win = win.getOtherWindow(false);
164       }
165     return win;
166   }
167
168   public void delete()
169   {
170     EFrame frame = this.frame;
171     deleteNoValidate();
172     if (frame.getFirstWindow() == null)
173       frame.delete();
174     else
175       frame.validate();
176   }
177
178   protected void deleteNoValidate()
179   {
180     if (frame.selectedWindow == this)
181       {
182         EWindow next = getNextWindowInFrame(1);
183         if (frame == EFrame.selectedFrame)
184           setSelected(next);
185         else
186           frame.selectedWindow = next;
187       }
188     unlink();
189     frame = null;
190   }
191
192   public void deleteOtherWindows()
193   {
194     for (EWindow cur = frame.getFirstWindow(); cur != null; )
195       {
196         EWindow next = cur.getNextWindow(true);
197         if (cur != this)
198           cur.deleteNoValidate();
199         cur = next;
200       }
201     frame.validate();
202   }
203
204   protected abstract void getCharSize();
205
206   /** Get the current width (in pixels) of this window. */
207   public abstract int getWidth ();
208
209   /** Get the current height (in pixels) of this window. */
210  public abstract int getHeight ();
211
212   public int getHeightInLines()
213   {
214     if (charHeight == 0)
215       getCharSize();
216     return getHeight() / charHeight;
217   }
218
219   public int getWidthInColumns()
220   {
221     if (charWidth == 0)
222       getCharSize();
223     return getWidth() / charWidth;
224   }
225
226   public String JavaDoc toString()
227   {
228     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(100);
229     sbuf.append("#<window on ");
230     if (buffer == null)
231       sbuf.append("no buffer");
232     else
233       {
234         sbuf.append('\"');
235         sbuf.append(buffer.getName());
236         sbuf.append('\"');
237       }
238     sbuf.append(" 0x");
239     sbuf.append(Integer.toHexString(System.identityHashCode(this)));
240     sbuf.append('>');
241     return sbuf.toString();
242   }
243
244   public void pushPrefix(int prefix)
245   {
246     if (pendingKeys == null)
247       pendingKeys = new int[10];
248     pendingKeys[pendingLength++] = prefix;
249   }
250
251   public Object JavaDoc lookupKey(int key)
252   {
253     for (int j = 0; j < buffer.activeLength; j++)
254       {
255         EKeymap actual = buffer.activeKeymaps[j];
256         Object JavaDoc action = actual.lookupKey(pendingKeys, pendingLength,
257                                   key, j < buffer.activeLength - 1);
258         if (action != null)
259       return action;
260       }
261     return EKeymap.ignorable(key) ? null : tooLong(pendingLength);
262   }
263
264   public abstract Object JavaDoc tooLong(int pendingLength);
265   
266   public void handleKey (int code)
267   {
268     Object JavaDoc command = lookupKey(code);
269
270     pushPrefix(code);
271     pendingLength--;
272     handleCommand (command);
273   }
274
275   public void handleCommand (Object JavaDoc command)
276   {
277     if (command instanceof String JavaDoc || command instanceof Symbol)
278       {
279     Object JavaDoc resolved = Command.resolveSymbol(command);
280     if (resolved == null)
281       throw new Error JavaDoc("no function defined for "+command);
282     command = resolved;
283       }
284
285     if (command instanceof EKeymap)
286       {
287      if (pendingKeys[pendingLength] != 0)
288        pendingLength++;
289      return;
290       }
291
292     pendingLength = 0;
293
294     try
295       {
296     Procedure proc = (Procedure) command;
297     Object JavaDoc interactive = proc.getProperty("emacs-interactive", null);
298     if (interactive != null)
299       {
300         if (interactive instanceof String JavaDoc)
301           {
302         proc.applyN(Command.processInteractionString(interactive.toString()));
303           }
304             else if (interactive == LList.Empty)
305               proc.apply0();
306         else
307           {
308         System.err.println("not implemented: interactive not a string");
309         proc.apply0();
310           }
311       }
312     else
313           {
314             // System.err.println("procedure "+proc+" is not interactive");
315
proc.apply0();
316           }
317       }
318     catch (CancelledException ex)
319       {
320     // Do nothing.
321
}
322     catch (RuntimeException JavaDoc ex)
323       {
324     throw ex;
325       }
326     catch (Error JavaDoc ex)
327       {
328     throw ex;
329       }
330     catch (Throwable JavaDoc ex)
331       {
332     throw new WrappedException(ex);
333       }
334
335   }
336 }
337
Popular Tags