38 * Class to paint a list of items, possibly with scrollbars
39 * This class paints all items with the same font
40 * For now, this class manages the list of items and painting thereof, but not
41 * posting of Item or ActionEvents
42 */
43 public class ListHelper implements XScrollbarClient {
44 private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.ListHelper");
45
46 private final int FOCUS_INSET = 1;
47
48 private final int BORDER_WIDTH; // Width of border drawn around the list
49 // of items
50 private final int ITEM_MARGIN; // Margin between the border of the list
51 // of items and and item's bg, and between
52 // items
53 private final int TEXT_SPACE; // Space between the edge of an item and
54 // the text
55
56 private final int SCROLLBAR_WIDTH; // Width of a scrollbar
57
58 private java.util.List items; // List of items
59
60 // TODO: maybe this would be better as a simple int[]
61 private java.util.List selected; // List of selected items
62 private boolean multiSelect; // Can multiple items be selected
63 // at once?
64 private int focusedIndex;
65
66 private int maxVisItems; // # items visible without a vsb
67 private XVerticalScrollbar vsb; // null if unsupported
68 private boolean vsbVis;
69 private XHorizontalScrollbar hsb; // null if unsupported
70 private boolean hsbVis;
71
72 private Font font;
73 private FontMetrics fm;
74
75 private XWindow peer; // So far, only needed for painting
76 // on notifyValue()
77 private Color[] colors; // Passed in for painting on notifyValue()
78
79 // Holds the true if mouse is dragging outside of the area of the list
80 // The flag is used at the moment of the dragging and releasing mouse
81 // See 6243382 for more information
83 private volatile boolean vsbVisibilityChanged = false;
84
85 /*
86 * Comment
87 */
88 public ListHelper(XWindow peer,
89 Color[] colors,
90 int initialSize,
91 boolean multiSelect,
92 boolean scrollVert,
93 boolean scrollHoriz,
94 Font font,
95 int maxVisItems,
96 int SPACE,
97 int MARGIN,
98 int BORDER,
99 int SCROLLBAR) {
100 this.peer = peer;
101 this.colors = colors;
102 this.multiSelect = multiSelect;
103 items = new ArrayList(initialSize);
104 selected = new ArrayList(1);
105 selected.add(Integer.valueOf(-1));
106
107 this.maxVisItems = maxVisItems;
108 if (scrollVert) {
109 vsb = new XVerticalScrollbar(this);
110 vsb.setValues(0, 0, 0, 0, 1, maxVisItems - 1);
111 }
112 if (scrollHoriz) {
113 hsb = new XHorizontalScrollbar(this);
114 hsb.setValues(0, 0, 0, 0, 1, 1);
115 }
116
117 setFont(font);
118 TEXT_SPACE = SPACE;
119 ITEM_MARGIN = MARGIN;
120 BORDER_WIDTH = BORDER;
121 SCROLLBAR_WIDTH = SCROLLBAR;
122 }
123
124 public Component getEventSource() {
173 }
174 if (multiSelect) {
175 assert false : "Implement ListHelper.select() for multiselect";
176 }
177 else if (getSelectedIndex() != index) {
178 selected.remove(0);
179 selected.add(Integer.valueOf(index));
180 makeVisible(index);
181 }
182 }
183
184 /* docs */
185 public void deselect(int index) {
186 assert(false);
187 }
188
189 /* docs */
190 /* if called for multiselect, return -1 */
191 public int getSelectedIndex() {
192 if (!multiSelect) {
193 Integer val = (Integer)selected.get(0);
194 return val.intValue();
195 }
196 return -1;
197 }
198
199 int[] getSelectedIndexes() { assert(false); return null;}
200
201 /*
202 * A getter method for XChoicePeer.
203 * Returns vsbVisiblityChanged value and sets it to false.
204 */
205 public boolean checkVsbVisibilityChangedAndReset(){
206 boolean returnVal = vsbVisibilityChanged;
207 vsbVisibilityChanged = false;
208 return returnVal;
209 }
210
211 public boolean isEmpty() {
212 return items.isEmpty();
213 }
214
215 public int getItemCount() {
216 return items.size();
217 }
218
219 public String getItem(int index) {
220 return (String) items.get(index);
221 }
222
223 /**********************************************************************/
224 /* GUI-related methods */
225 /**********************************************************************/
226
227 public void setFocusedIndex(int index) {
228 focusedIndex = index;
229 }
230
231 public boolean isFocusedIndex(int index) {
232 return index == focusedIndex;
233 }
234
235 public void setFont(Font newFont) {
236 if (newFont != font) {
237 font = newFont;
238 fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
239 // Also cache stuff like fontHeight?
240 }
559
560 if (selected) {
561 g.setColor(colors[XComponentPeer.BACKGROUND_COLOR]);
562 }
563 else {
564 g.setColor(colors[XComponentPeer.FOREGROUND_COLOR]);
565 }
566 g.setFont(font);
567 //Rectangle clip = g.getClipBounds();
568 //g.clipRect(x, y, width, height);
569 //g.drawString(string, x + TEXT_SPACE, y + TEXT_SPACE + ITEM_MARGIN);
570
571 int fontAscent = fm.getAscent();
572 int fontDescent = fm.getDescent();
573
574 g.drawString(string, x + TEXT_SPACE, y + (height + fm.getMaxAscent() - fm.getMaxDescent())/2);
575 //g.clipRect(clip.x, clip.y, clip.width, clip.height);
576 }
577
578 boolean isItemSelected(int index) {
579 Iterator itr = selected.iterator();
580 while (itr.hasNext()) {
581 Integer val = (Integer)itr.next();
582 if (val.intValue() == index) {
583 return true;
584 }
585 }
586 return false;
587 }
588
589 public void paintVSB(Graphics g, Color colors[], Rectangle bounds) {
590 int height = bounds.height - 2*BORDER_WIDTH - (hsbVis ? (SCROLLBAR_WIDTH-2) : 0);
591 Graphics ng = g.create();
592
593 g.setColor(colors[XComponentPeer.BACKGROUND_COLOR]);
594 try {
595 ng.translate(bounds.width - BORDER_WIDTH - SCROLLBAR_WIDTH,
596 BORDER_WIDTH);
597 // Update scrollbar's size
598 vsb.setSize(SCROLLBAR_WIDTH, bounds.height);
599 vsb.paint(ng, colors, true);
600 } finally {
601 ng.dispose();
|
38 * Class to paint a list of items, possibly with scrollbars
39 * This class paints all items with the same font
40 * For now, this class manages the list of items and painting thereof, but not
41 * posting of Item or ActionEvents
42 */
43 public class ListHelper implements XScrollbarClient {
44 private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.ListHelper");
45
46 private final int FOCUS_INSET = 1;
47
48 private final int BORDER_WIDTH; // Width of border drawn around the list
49 // of items
50 private final int ITEM_MARGIN; // Margin between the border of the list
51 // of items and and item's bg, and between
52 // items
53 private final int TEXT_SPACE; // Space between the edge of an item and
54 // the text
55
56 private final int SCROLLBAR_WIDTH; // Width of a scrollbar
57
58 private java.util.List<String> items; // List of items
59
60 // TODO: maybe this would be better as a simple int[]
61 private java.util.List<Integer> selected; // List of selected items
62 private boolean multiSelect; // Can multiple items be selected
63 // at once?
64 private int focusedIndex;
65
66 private int maxVisItems; // # items visible without a vsb
67 private XVerticalScrollbar vsb; // null if unsupported
68 private boolean vsbVis;
69 private XHorizontalScrollbar hsb; // null if unsupported
70 private boolean hsbVis;
71
72 private Font font;
73 private FontMetrics fm;
74
75 private XWindow peer; // So far, only needed for painting
76 // on notifyValue()
77 private Color[] colors; // Passed in for painting on notifyValue()
78
79 // Holds the true if mouse is dragging outside of the area of the list
80 // The flag is used at the moment of the dragging and releasing mouse
81 // See 6243382 for more information
83 private volatile boolean vsbVisibilityChanged = false;
84
85 /*
86 * Comment
87 */
88 public ListHelper(XWindow peer,
89 Color[] colors,
90 int initialSize,
91 boolean multiSelect,
92 boolean scrollVert,
93 boolean scrollHoriz,
94 Font font,
95 int maxVisItems,
96 int SPACE,
97 int MARGIN,
98 int BORDER,
99 int SCROLLBAR) {
100 this.peer = peer;
101 this.colors = colors;
102 this.multiSelect = multiSelect;
103 items = new ArrayList<>(initialSize);
104 selected = new ArrayList<>(1);
105 selected.add(Integer.valueOf(-1));
106
107 this.maxVisItems = maxVisItems;
108 if (scrollVert) {
109 vsb = new XVerticalScrollbar(this);
110 vsb.setValues(0, 0, 0, 0, 1, maxVisItems - 1);
111 }
112 if (scrollHoriz) {
113 hsb = new XHorizontalScrollbar(this);
114 hsb.setValues(0, 0, 0, 0, 1, 1);
115 }
116
117 setFont(font);
118 TEXT_SPACE = SPACE;
119 ITEM_MARGIN = MARGIN;
120 BORDER_WIDTH = BORDER;
121 SCROLLBAR_WIDTH = SCROLLBAR;
122 }
123
124 public Component getEventSource() {
173 }
174 if (multiSelect) {
175 assert false : "Implement ListHelper.select() for multiselect";
176 }
177 else if (getSelectedIndex() != index) {
178 selected.remove(0);
179 selected.add(Integer.valueOf(index));
180 makeVisible(index);
181 }
182 }
183
184 /* docs */
185 public void deselect(int index) {
186 assert(false);
187 }
188
189 /* docs */
190 /* if called for multiselect, return -1 */
191 public int getSelectedIndex() {
192 if (!multiSelect) {
193 Integer val = selected.get(0);
194 return val.intValue();
195 }
196 return -1;
197 }
198
199 int[] getSelectedIndexes() { assert(false); return null;}
200
201 /*
202 * A getter method for XChoicePeer.
203 * Returns vsbVisiblityChanged value and sets it to false.
204 */
205 public boolean checkVsbVisibilityChangedAndReset(){
206 boolean returnVal = vsbVisibilityChanged;
207 vsbVisibilityChanged = false;
208 return returnVal;
209 }
210
211 public boolean isEmpty() {
212 return items.isEmpty();
213 }
214
215 public int getItemCount() {
216 return items.size();
217 }
218
219 public String getItem(int index) {
220 return items.get(index);
221 }
222
223 /**********************************************************************/
224 /* GUI-related methods */
225 /**********************************************************************/
226
227 public void setFocusedIndex(int index) {
228 focusedIndex = index;
229 }
230
231 public boolean isFocusedIndex(int index) {
232 return index == focusedIndex;
233 }
234
235 public void setFont(Font newFont) {
236 if (newFont != font) {
237 font = newFont;
238 fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
239 // Also cache stuff like fontHeight?
240 }
559
560 if (selected) {
561 g.setColor(colors[XComponentPeer.BACKGROUND_COLOR]);
562 }
563 else {
564 g.setColor(colors[XComponentPeer.FOREGROUND_COLOR]);
565 }
566 g.setFont(font);
567 //Rectangle clip = g.getClipBounds();
568 //g.clipRect(x, y, width, height);
569 //g.drawString(string, x + TEXT_SPACE, y + TEXT_SPACE + ITEM_MARGIN);
570
571 int fontAscent = fm.getAscent();
572 int fontDescent = fm.getDescent();
573
574 g.drawString(string, x + TEXT_SPACE, y + (height + fm.getMaxAscent() - fm.getMaxDescent())/2);
575 //g.clipRect(clip.x, clip.y, clip.width, clip.height);
576 }
577
578 boolean isItemSelected(int index) {
579 Iterator<Integer> itr = selected.iterator();
580 while (itr.hasNext()) {
581 Integer val = itr.next();
582 if (val.intValue() == index) {
583 return true;
584 }
585 }
586 return false;
587 }
588
589 public void paintVSB(Graphics g, Color colors[], Rectangle bounds) {
590 int height = bounds.height - 2*BORDER_WIDTH - (hsbVis ? (SCROLLBAR_WIDTH-2) : 0);
591 Graphics ng = g.create();
592
593 g.setColor(colors[XComponentPeer.BACKGROUND_COLOR]);
594 try {
595 ng.translate(bounds.width - BORDER_WIDTH - SCROLLBAR_WIDTH,
596 BORDER_WIDTH);
597 // Update scrollbar's size
598 vsb.setSize(SCROLLBAR_WIDTH, bounds.height);
599 vsb.paint(ng, colors, true);
600 } finally {
601 ng.dispose();
|