summaryrefslogtreecommitdiff
path: root/NET/worlds/console/TreePanel.java
blob: 3737f20f36f584192e6ea66ade7f57709d61b34d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package NET.worlds.console;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Event;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.Scrollbar;
import java.util.Vector;

public class TreePanel extends ExposedPanel implements DialogDisabled {
   private static final long serialVersionUID = -5534860983354876334L;
   private Vector<TreeNode> items = new Vector<TreeNode>();
   private boolean delayRepaints;
   private boolean needRepaint;
   private boolean needRecalc;
   private boolean needMakeVisible;
   private Scrollbar scrollbar = new WiderScrollbar();
   private int scrollPos = 0;
   private int linesVisible;
   private int selectedIndex = -1;
   private boolean hasFocus = true;
   private Font nFont = new Font(Console.message("TreeFont"), 0, 13);
   private FontMetrics nFontMetrics = this.getFontMetrics(this.nFont);
   private Font bFont = new Font(Console.message("TreeFont"), 1, 11);
   private FontMetrics bFontMetrics = this.getFontMetrics(this.bFont);
   private int fontHeight = Math.max(this.nFontMetrics.getHeight(), this.bFontMetrics.getHeight());
   private int itemHeight = this.fontHeight;
   private boolean isDialogDisabled;
   private static final int[] cxs = new int[]{0, 0, 6};
   private static final int[] cys = new int[]{6, -6, 0};
   private MoveablePolygon closedIcon = new MoveablePolygon(cxs, cys);
   private static final int[] oxs = new int[]{-5, 6, 0};
   private static final int[] oys = new int[]{0, 0, 6};
   private MoveablePolygon openedIcon = new MoveablePolygon(oxs, oys);
   private static final Color normBGColor = new Color(80, 80, 80);
   private static final Color normFGColor = new Color(190, 190, 190);
   private static final Color selFGColor = new Color(255, 255, 175);
   private static final int indentPixels = 14;

   public TreePanel() {
      this.setBackground(normBGColor);
      this.setLayout(new BorderLayout());
      this.add("East", this.scrollbar);
      this.scrollbar.hide();
   }

   public synchronized void delayRepaints(boolean state) {
      if (!(this.delayRepaints = state)) {
         if (this.needRecalc) {
            this.recalc();
         }

         if (this.needMakeVisible) {
            this.makeVisible(this.selectedIndex);
         }

         if (this.needRepaint) {
            this.repaint();
         }
      }
   }

   private synchronized void needRepaint(boolean needRecalc, boolean needMakeVisible) {
      this.needRepaint = true;
      this.needRecalc |= needRecalc;
      this.needMakeVisible |= needMakeVisible;
      this.delayRepaints(this.delayRepaints);
   }

   public synchronized int getSelectedIndex() {
      return this.selectedIndex;
   }

   public synchronized void select(int item) {
      this.selectedIndex = item;
      this.needRepaint(false, true);
   }

   public void setFocus(boolean hasFocus) {
      if (this.hasFocus != hasFocus) {
         this.hasFocus = hasFocus;
         this.needRepaint(false, false);
      }
   }

   @Override
   public boolean hasFocus() {
      return this.hasFocus;
   }

   @Override
   public synchronized void reshape(int x, int y, int w, int h) {
      super.reshape(x, y, w, h);
      this.needRepaint(true, false);
   }

   public synchronized TreeNode getSelectedNode() {
      return this.selectedIndex != -1 ? this.items.elementAt(this.selectedIndex) : null;
   }

   public synchronized void reset(Vector<TreeNode> items) {
      this.items = items;
      this.needRepaint(true, false);
   }

   public synchronized void removeAllElements() {
      this.items.removeAllElements();
      this.needRepaint(true, false);
   }

   public synchronized void insertElementAt(TreeNode ele, int index) {
      this.items.insertElementAt(ele, index);
      this.needRepaint(true, false);
   }

   public synchronized void removeElementAt(int index) {
      this.items.removeElementAt(index);
      this.needRepaint(true, false);
   }

   public synchronized void addElement(TreeNode ele) {
      this.items.addElement(ele);
      this.needRepaint(true, false);
   }

   public int countElements() {
      return this.items.size();
   }

   public TreeNode elementAt(int index) {
      return this.items.elementAt(index);
   }

   public TreeNode elementAt(Point p) {
      int item = this.scrollPos + p.y / this.itemHeight;
      return item < this.items.size() ? this.items.elementAt(item) : null;
   }

   private void makeVisible(int item) {
      int count = this.items.size();
      if (count > this.linesVisible && (item < this.scrollPos || item >= this.scrollPos + this.linesVisible)) {
         this.setScrollValue(Math.min(item * this.itemHeight, this.scrollbar.getMaximum()));
      }

      this.needMakeVisible = false;
   }

   private void add(GridBagLayout gbag, Component comp, GridBagConstraints c) {
      gbag.setConstraints(comp, c);
      this.add(comp);
   }

   public synchronized void recalc() {
      int height = this.getSize().height;
      this.linesVisible = Math.max(1, height / this.itemHeight);
      int count = this.items.size();
      if (count > this.linesVisible) {
         if (!this.scrollbar.isVisible()) {
            this.scrollbar.setVisible(true);
            this.validate();
         }

         this.scrollbar.setValues(this.scrollPos * this.itemHeight, this.linesVisible * this.itemHeight, 0, count * this.itemHeight);
         this.scrollbar.setPageIncrement(this.linesVisible * this.itemHeight);
         this.scrollbar.setLineIncrement(this.itemHeight);
      } else {
         if (this.scrollbar.isVisible()) {
            this.scrollbar.hide();
            this.validate();
         }

         this.scrollPos = 0;
      }

      this.needRecalc = false;
   }

   @Override
   public synchronized void paint(Graphics g) {
      super.paint(g);
      int width = this.getSize().width;
      int height = this.getSize().height;
      int count = this.items.size();
      int y = 0;

      for (int i = this.scrollPos; i < count; i++) {
         TreeNode node = this.elementAt(i);
         String name = node.toString();
         boolean isTitle = node.displayAsTitle();
         Font font = this.nFont;
         FontMetrics metrics = this.nFontMetrics;
         if (isTitle) {
            font = this.bFont;
            metrics = this.bFontMetrics;
         }

         g.setFont(font);
         int ascent = metrics.getAscent();
         int x = node.getLevel() * 14;
         g.setColor(normFGColor);
         if (node.isOpen()) {
            this.openedIcon.drawFilled(g, x + 5, y + ascent - 5);
         } else {
            this.closedIcon.drawFilled(g, x + 5, y + ascent - 5);
         }

         if (i == this.selectedIndex) {
            g.setColor(selFGColor);
         }

         g.drawString(name, x + 11 + 5, y + ascent);
         if (i == this.selectedIndex && this.hasFocus) {
            g.drawRect(x + 11 + 3, y, metrics.stringWidth(name) + 3, this.itemHeight);
         }

         y += this.itemHeight;
         if (y >= height) {
            break;
         }
      }

      this.needRepaint = false;
   }

   @Override
   public boolean handleEvent(Event e) {
      if (this.isDialogDisabled) {
         return false;
      } else {
         switch (e.id) {
            case 601:
               return this.scrollLineUp();
            case 602:
               return this.scrollLineDown();
            case 603:
               return this.scrollPageUp();
            case 604:
               return this.scrollPageDown();
            case 605:
               return this.scrollAbsolute();
            default:
               return super.handleEvent(e);
         }
      }
   }

   @Override
   public void dialogDisable(boolean disable) {
      this.isDialogDisabled = disable;
   }

   @Override
   public synchronized boolean mouseDown(Event e, int x, int y) {
      this.setFocus(true);
      int item = this.scrollPos + y / this.itemHeight;
      if (item >= 0 && item < this.items.size()) {
         if (e.clickCount == 1) {
            this.treeSelect(item);
            int yStart = y / this.itemHeight * this.itemHeight;
            TreeNode node = this.elementAt(item);
            MoveablePolygon widget = node.isOpen() ? this.openedIcon : this.closedIcon;
            FontMetrics metrics = node.displayAsTitle() ? this.nFontMetrics : this.bFontMetrics;
            widget.moveTo(node.getLevel() * 14 + 5, yStart + metrics.getAscent() - 5);
            if (widget.getBoundingBox().inside(x, y)) {
               this.treeOpen(item);
            }
         } else {
            this.treeOpen(item);
         }
      }

      return true;
   }

   public void treeSelect(int item) {
   }

   public void treeOpen(int item) {
   }

   private boolean setScrollValue(int value) {
      value = Math.max(this.scrollbar.getMinimum(), value);
      value = Math.min(this.scrollbar.getMaximum(), value);
      this.scrollPos = value / this.itemHeight;
      this.scrollbar.setValue(this.scrollPos * this.itemHeight);
      this.needRepaint(false, false);
      return true;
   }

   private boolean scrollLineUp() {
      return this.setScrollValue(this.scrollPos * this.itemHeight - this.scrollbar.getLineIncrement());
   }

   private boolean scrollLineDown() {
      return this.setScrollValue(this.scrollPos * this.itemHeight + this.scrollbar.getLineIncrement());
   }

   private boolean scrollPageUp() {
      return this.setScrollValue(this.scrollPos * this.itemHeight - this.scrollbar.getPageIncrement());
   }

   private boolean scrollPageDown() {
      return this.setScrollValue(this.scrollPos * this.itemHeight + this.scrollbar.getPageIncrement());
   }

   private boolean scrollAbsolute() {
      return this.setScrollValue(this.scrollbar.getValue());
   }
}