001: /**
002: * Caption: Zaval Java Resource Editor
003: * $Revision: 0.37 $
004: * $Date: 2002/03/28 9:24:42 $
005: *
006: * @author: Victor Krapivin
007: * @version: 1.3
008: *
009: * Zaval JRC Editor is a visual editor which allows you to manipulate
010: * localization strings for all Java based software with appropriate
011: * support embedded.
012: *
013: * For more info on this product read Zaval Java Resource Editor User's Guide
014: * (It comes within this package).
015: * The latest product version is always available from the product's homepage:
016: * http://www.zaval.org/products/jrc-editor/
017: * and from the SourceForge:
018: * http://sourceforge.net/projects/zaval0002/
019: *
020: * Contacts:
021: * Support : support@zaval.org
022: * Change Requests : change-request@zaval.org
023: * Feedback : feedback@zaval.org
024: * Other : info@zaval.org
025: *
026: * Copyright (C) 2001-2002 Zaval Creative Engineering Group (http://www.zaval.org)
027: *
028: * This program is free software; you can redistribute it and/or
029: * modify it under the terms of the GNU General Public License
030: * (version 2) as published by the Free Software Foundation.
031: *
032: * This program is distributed in the hope that it will be useful,
033: * but WITHOUT ANY WARRANTY; without even the implied warranty of
034: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
035: * GNU General Public License for more details.
036: *
037: * You should have received a copy of the GNU General Public License
038: * along with this program; if not, write to the Free Software
039: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
040: *
041: */package org.zaval.awt;
042:
043: import java.awt.*;
044: import java.applet.*;
045: import java.util.*;
046:
047: public class ContextMenu extends Menu {
048: Rectangle size = null;
049: int htext = 0;
050: int stap = 0;
051: int vdist = 0;
052: int hdist = 0;
053: int msize = 0;
054: int act_option = 0;
055: int prev_option = 0;
056: int index = -1;
057: boolean act = false;
058: Color col_mark = new Color(128);
059: Hashtable types = new Hashtable();
060:
061: public ContextMenu(String name) {
062: super (name);
063: this .setFont(new Font("Dialog", Font.PLAIN, 12));
064: }
065:
066: // ====================================================================
067:
068: public ContextMenu(String name, int x, int y) {
069: this (name);
070: size.x = x;
071: size.y = y;
072: }
073:
074: // ====================================================================
075:
076: public boolean isActive() {
077: return act;
078: }
079:
080: // ====================================================================
081:
082: public void setActive(boolean a_act) {
083: act = a_act;
084: }
085:
086: // ====================================================================
087:
088: public void addCheckit(MenuItem mi, boolean state) {
089: this .add(mi);
090: if (state)
091: types.put(mi.getLabel(), "1");
092: else
093: types.put(mi.getLabel(), "0");
094: }
095:
096: // ====================================================================
097:
098: public void addCheckit(String name, boolean state) {
099: this .addCheckit(new MenuItem(name), state);
100: }
101:
102: // ====================================================================
103:
104: public void add(String name) {
105: this .add(new MenuItem(name));
106: }
107:
108: // ====================================================================
109:
110: public void addSeparator() {
111: super .addSeparator();
112: }
113:
114: // ====================================================================
115:
116: public void remove(int index) {
117: size.height -= stap;
118: super .remove(index);
119: }
120:
121: // ====================================================================
122:
123: public void remove(MenuComponent mc) {
124: super .remove(mc);
125: }
126:
127: // ====================================================================
128:
129: public MenuItem add(MenuItem mi) {
130: MenuItem ret_mi = super .add(mi);
131: FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(
132: getFont());
133: int sx = fm.stringWidth(mi.getLabel()) + 2 * hdist;
134:
135: if (size.width < sx)
136: size.width = sx;
137: size.height += stap;
138: return ret_mi;
139: }
140:
141: // ====================================================================
142:
143: public void recalc() {
144: recalc(getFont());
145: }
146:
147: // ====================================================================
148:
149: public Dimension getSize() {
150: return new Dimension(size.width, size.height);
151: }
152:
153: // ====================================================================
154:
155: public Rectangle getBounds() {
156: return new Rectangle(size.x, size.y, size.width, size.height);
157: }
158:
159: // ====================================================================
160:
161: public Dimension preferredSize() {
162: return new Dimension(size.width + 2 * hdist, size.height + 2
163: * vdist);
164: }
165:
166: // ====================================================================
167:
168: public void recalc(Font fnt) {
169: FontMetrics fm = Toolkit.getDefaultToolkit()
170: .getFontMetrics(fnt);
171: hdist = fm.stringWidth("IIIII");
172: htext = fm.getMaxAscent() + fm.getMaxDescent()
173: + fm.getLeading();
174: vdist = htext / 4;
175: stap = vdist + htext;
176: msize = htext + htext / 4;
177: if (size == null)
178: size = new Rectangle(0, 0, 0, 2 * vdist);
179: else
180: size.height = vdist + 5;
181: int count = countItems();
182: MenuItem mi[] = new MenuItem[count];
183: for (int i = 0; i < count; i++) {
184: mi[i] = getItem(i);
185: remove(i);
186: }
187: for (int i = 0; i < count; i++)
188: add(mi[i]);
189: }
190:
191: // ====================================================================
192:
193: public void setFont(Font fnt) {
194: super .setFont(fnt);
195: recalc();
196: }
197:
198: // ====================================================================
199:
200: private void drawSeparator(Graphics gr, int y) {
201: int x1 = size.x + 2;
202: int x2 = size.x + size.width - 3;
203: y = y - vdist;
204: gr.setColor(Color.gray);
205: gr.drawLine(x1, y - 1, x2, y - 1);
206: gr.setColor(Color.white);
207: gr.drawLine(x1, y, x2, y);
208: }
209:
210: // ====================================================================
211:
212: public Rectangle getRedrawArea() {
213: int dy = size.y + vdist + vdist;
214: if (prev_option > act_option)
215: return new Rectangle(size.x, dy + act_option * stap,
216: size.width, 2 * msize);
217: else
218: return new Rectangle(size.x, dy + prev_option * stap,
219: size.width, 2 * msize);
220: }
221:
222: // ====================================================================
223:
224: private void correctPos() {
225: if (getParent() == null)
226: return;
227: Dimension d = ((ContextMenuBar) (getParent())).getParentSize();
228: if (size.x < hdist)
229: size.x = hdist;
230: if (size.y < vdist)
231: size.y = vdist;
232: int dx = size.x + size.width + hdist;
233: int dy = size.y + size.height + vdist;
234: if (dx > d.width)
235: size.x -= (dx - d.width);
236: if (dy > d.height)
237: size.y -= (dy - d.height);
238: if (size.x < 0)
239: size.x = 0;
240: if (size.y < 0)
241: size.y = 0;
242: }
243:
244: // ====================================================================
245:
246: public boolean paint(Graphics gr) {
247: if (!isEnabled())
248: return false;
249: correctPos();
250:
251: gr.setFont(getFont());
252:
253: gr.setColor(Color.lightGray);
254: gr.fillRect(size.x, size.y, size.width, size.height);
255:
256: gr.setColor(Color.darkGray);
257: gr.drawRect(size.x, size.y, size.width - 1, size.height - 1);
258:
259: gr.setColor(Color.white);
260: gr.drawLine(size.x, size.y, size.x + size.width, size.y);
261: gr.drawLine(size.x, size.y, size.x, size.y + size.height - 1);
262:
263: gr.setColor(col_mark);
264: gr.fillRect(size.x + 3, size.y + vdist + act_option * stap,
265: size.width - 6, msize);
266:
267: int x = size.x + hdist;
268: int y = size.y + stap;
269: int count = countItems();
270: for (int i = 0; i < count; i++)
271: drawOption(gr, i);
272: return true;
273: }
274:
275: // ====================================================================
276:
277: /* private void drawCheckit(Graphics gr, int index, String name, int x, int y)
278: {
279: String state = (String)types.get(name);
280: if (state!=null)
281: {
282: ContextMenuBar cmb = (ContextMenuBar)(getParent());
283: if (state.equals("1"))
284: if (index == act_option)
285: gr.drawImage(mon , x, y, col_mark, cmb.parent);
286: else
287: gr.drawImage(on , x, y, col_mark, cmb.parent);
288: // else gr.drawImage(off, size.x + hdist/4, size.y + vdist + add, cmb.parent);
289: }
290: }*/
291:
292: // ====================================================================
293: private void drawCheckit(Graphics gr, int index, String name,
294: int x, int y) {
295: String state = (String) types.get(name);
296: if (state != null) {
297: ContextMenuBar cmb = (ContextMenuBar) (getParent());
298: if (state.equals("1"))
299: for (int i = 0; i < 2; i++) {
300: gr.drawLine(x + hdist / 3 + i, y + stap - htext
301: + vdist, x + hdist / 2 + i, y + stap);
302: gr.drawLine(x + hdist / 2 + i, y + stap, x + hdist
303: - 3 + i, y + stap - htext + vdist);
304: }
305: // else gr.drawImage(off, size.x + hdist/4, size.y + vdist + add, cmb.parent);
306: }
307: }
308:
309: // ====================================================================
310:
311: private void drawOption(Graphics gr, int index) {
312: if (index >= countItems())
313: return;
314: MenuItem opt = getItem(index);
315: String name = opt.getLabel();
316: int y = size.y + stap;
317: int x = size.x + hdist;
318: int add = index * stap;
319: if (opt.isEnabled()) {
320: if (name.equals("-")) {
321: drawSeparator(gr, y + add);
322: return;
323: }
324: if (index != act_option)
325: gr.setColor(Color.black);
326: else
327: gr.setColor(Color.white);
328: drawCheckit(gr, index, name, size.x, size.y + add);
329: gr.drawString(name, x, y + add);
330: } else {
331: gr.setColor(Color.darkGray);
332: gr.drawString(name, x, y + add);
333: gr.setColor(Color.white);
334: gr.drawString(name, x + 1, y + 1 + add);
335: if (index != act_option)
336: gr.setColor(Color.darkGray);
337: drawCheckit(gr, index, name, size.x, size.y + add);
338: }
339:
340: }
341:
342: // ====================================================================
343:
344: public boolean paintPart(Graphics gr) {
345: if (!isEnabled())
346: return false;
347: correctPos();
348:
349: gr.setFont(getFont());
350:
351: gr.setColor(Color.lightGray);
352: gr.fillRect(size.x + 1, size.y + vdist + prev_option * stap,
353: size.width - 2, msize);
354: drawOption(gr, prev_option);
355:
356: gr.setColor(col_mark);
357: gr.fillRect(size.x + 3, size.y + vdist + act_option * stap,
358: size.width - 6, msize);
359: drawOption(gr, act_option);
360: return true;
361: }
362:
363: // ====================================================================
364:
365: public boolean paint(Graphics gr, int x, int y) {
366: setPos(x, y);
367: return paint(gr);
368: }
369:
370: // ====================================================================
371:
372: public void setPos(int x, int y) {
373: size.x = x;
374: size.y = y;
375: }
376:
377: // ====================================================================
378:
379: public void pressKey(Event evt) {
380: int num = countItems();
381: boolean flag = true;
382: if (num == 0)
383: return;
384: switch (evt.key) {
385: case Event.UP:
386: flag = decOption();
387: break;
388: case Event.DOWN:
389: flag = incOption();
390: break;
391: }
392: if (flag)
393: sendEvent(ContextMenuBar.EV_MENU_REDRAW);
394: }
395:
396: // ====================================================================
397:
398: public boolean isCheckit(int act) {
399: MenuItem mi = getItem(act);
400: if (mi == null)
401: return false;
402: if (types.get(mi.getLabel()) != null)
403: return true;
404: return false;
405: }
406:
407: // ====================================================================
408:
409: public void invCheckit(int act) {
410: if (!isCheckit(act))
411: return;
412: String state = (String) types.get(getItem(act).getLabel());
413: if (state.equals("1"))
414: types.put(getItem(act).getLabel(), "0");
415: else
416: types.put(getItem(act).getLabel(), "1");
417: }
418:
419: // ====================================================================
420:
421: public void pressEnter() {
422: MenuItem mi = getItem(act_option);
423: if (mi.isEnabled()) {
424: invCheckit(act_option);
425: sendEvent(ContextMenuBar.EV_MENU_ENTER);
426: }
427: }
428:
429: // ====================================================================
430:
431: public void pressExit() {
432: sendEvent(ContextMenuBar.EV_MENU_EXIT);
433: }
434:
435: // ====================================================================
436:
437: private boolean incOption() {
438: int num = countItems();
439: if (num <= 0)
440: return false;
441: prev_option = act_option;
442: for (int i = 0; i < num; i++)
443: if (act_option < (num - 1)) {
444: act_option++;
445: if (getItem(act_option).getLabel().equals("-"))
446: continue;
447: return true;
448: } else {
449: act_option = 0;
450: if (getItem(act_option).getLabel().equals("-"))
451: continue;
452: return true;
453: }
454: return false;
455: }
456:
457: // ====================================================================
458:
459: private boolean decOption() {
460: int num = countItems();
461: if (num <= 0)
462: return false;
463: prev_option = act_option;
464: for (int i = 0; i < num; i++)
465: if (act_option > 0) {
466: act_option--;
467: if (getItem(act_option).getLabel().equals("-"))
468: continue;
469: return true;
470: } else {
471: act_option = (num - 1);
472: if (getItem(act_option).getLabel().equals("-"))
473: continue;
474: return true;
475: }
476: return false;
477: }
478:
479: // ====================================================================
480:
481: public void pressMouse(Event evt) {
482: int num = countItems();
483: boolean flag = true;
484: if ((num == 0) || (!this .inside(evt.x, evt.y))) {
485: if (evt.clickCount >= 1)
486: pressExit();
487: return;
488: }
489: int option = (evt.y - size.y) / stap;
490: if ((option != act_option) && (option < num)
491: && (!getItem(option).getLabel().equals("-"))) {
492: prev_option = act_option;
493: act_option = option;
494: } else
495: flag = false;
496: if (flag)
497: sendEvent(ContextMenuBar.EV_MENU_REDRAW);
498: if ((evt.id == Event.MOUSE_UP))
499: pressEnter();
500: }
501:
502: // ====================================================================
503:
504: public boolean inside(int x, int y) {
505: if ((x > (size.x + size.width)) || (x < size.x)
506: || (y > (size.y + size.height)) || (y < size.y))
507: return false;
508: return true;
509: }
510:
511: // ====================================================================
512:
513: // int isLeftKey = 0;
514: public boolean handleEvent(Event evt) {
515: switch (evt.id) {
516: case Event.MOUSE_EXIT:
517: pressExit();
518: break;
519: case Event.KEY_PRESS:
520: if (evt.key == 10)
521: pressEnter();
522: else
523: pressExit();
524: break;
525: case Event.KEY_ACTION:
526: pressKey(evt);
527: break;
528: case Event.MOUSE_DRAG:
529: ;
530: case Event.MOUSE_MOVE:
531: pressMouse(evt);
532: break;
533: case Event.MOUSE_DOWN: {
534: /*if (evt.modifiers != 4) isLeftKey = 1;
535: else isLeftKey = 1;*/
536: pressMouse(evt);
537: }
538: break;
539: case Event.MOUSE_UP: //if (isLeftKey == 1)
540: {
541: pressMouse(evt);
542: //isLeftKey = 0;
543: }
544: break;
545: }
546: return true; //super.handleEvent(evt);
547: }
548:
549: // ====================================================================
550:
551: private void sendEvent(int id) {
552: if (getParent() == null)
553: return;
554: Event evt = null;
555: switch (id) {
556: case ContextMenuBar.EV_MENU_REDRAW:
557: evt = new Event(null, ContextMenuBar.EV_MENU_REDRAW, null);
558: break;
559: case ContextMenuBar.EV_MENU_REDRAWALL:
560: evt = new Event(null, ContextMenuBar.EV_MENU_REDRAWALL,
561: null);
562: break;
563: case ContextMenuBar.EV_MENU_EXIT:
564: evt = new Event(null, ContextMenuBar.EV_MENU_EXIT, null);
565: break;
566: case ContextMenuBar.EV_MENU_ENTER: {
567: MenuItem trg = getItem(act_option);
568: evt = new Event(trg, ContextMenuBar.EV_MENU_ENTER, trg
569: .getLabel());
570: }
571: break;
572: }
573: if (evt != null)
574: ((ContextMenuBar) (getParent())).sendEvent(evt);
575: }
576:
577: // ====================================================================
578:
579: }
|