summaryrefslogtreecommitdiff
path: root/NET/worlds/console/BookmarkDeleteDialog.java
blob: c88e8b5cdeb862ed4fb0a170975324f65b2f9920 (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
package NET.worlds.console;

import java.awt.Button;
import java.awt.Event;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.List;

class BookmarkDeleteDialog extends PolledDialog implements DialogReceiver {
   private static final long serialVersionUID = 7297733090895837320L;
   private List listbox = new List(10);
   private Button delButton = new Button(Console.message("Delete"));
   private Button cancelButton = new Button(Console.message("Done"));
   private WorldsMarkPart bookmarks;
   private static Font font = new Font(Console.message("MenuFont"), 0, 12);
   private static Font bfont = new Font(Console.message("ButtonFont"), 0, 12);

   BookmarkDeleteDialog(WorldsMarkPart bookmarks) {
      super(Console.getFrame(), null, Console.message("Delete-WorldsMark"), true);
      this.bookmarks = bookmarks;
      this.ready();
   }

   @Override
   protected void build() {
      int count = WorldsMarkPart.getBookmarkCount();

      for (int i = 0; i < count; i++) {
         this.listbox.add(WorldsMarkPart.getBookmarkName(i));
      }

      Label caption = new Label(Console.message("Choose-WorldsMark"));
      GridBagLayout gbag = new GridBagLayout();
      this.setLayout(gbag);
      GridBagConstraints c = new GridBagConstraints();
      c.fill = 2;
      c.gridwidth = 0;
      c.gridheight = 1;
      c.weightx = 1.0;
      c.weighty = 0.0;
      caption.setFont(font);
      this.add(gbag, caption, c);
      c.fill = 1;
      c.gridwidth = 0;
      c.gridheight = 6;
      c.weightx = 1.0;
      c.weighty = 1.0;
      this.listbox.setFont(font);
      this.add(gbag, this.listbox, c);
      c.fill = 0;
      c.gridwidth = -1;
      c.gridheight = 0;
      c.anchor = 14;
      c.weightx = 0.45;
      c.weighty = 0.0;
      this.delButton.setFont(bfont);
      this.cancelButton.setFont(bfont);
      this.add(gbag, this.delButton, c);
      c.gridwidth = 0;
      c.anchor = 16;
      c.weightx = 0.55;
      this.add(gbag, this.cancelButton, c);
   }

   private void select(boolean state) {
      this.delButton.setEnabled(state);
   }

   @Override
   public void show() {
      super.show();
      if (this.listbox.getItemCount() != 0) {
         this.listbox.select(0);
         this.select(true);
      } else {
         this.select(false);
      }

      this.listbox.requestFocus();
   }

   @Override
   public boolean handleEvent(Event event) {
      if (event.id == 701) {
         this.select(true);
      } else if (event.id == 702) {
         this.select(false);
      }

      return super.handleEvent(event);
   }

   @Override
   public boolean action(Event event, Object what) {
      Object target = event.target;
      if (target == this.cancelButton) {
         return this.done(false);
      } else if (target == this.delButton) {
         int index = this.listbox.getSelectedIndex();
         if (index != -1) {
            this.listbox.remove(index);
            this.bookmarks.removeBookmark(index);
            int count = this.listbox.getItemCount();
            if (index < count - 1) {
               this.listbox.select(index);
            } else if (count > 0) {
               this.listbox.select(count - 1);
            } else {
               this.select(false);
               this.listbox.requestFocus();
            }
         }

         return true;
      } else {
         return false;
      }
   }

   @Override
   public boolean keyDown(Event event, int key) {
      return key != 27 && key != 10 ? super.keyDown(event, key) : this.done(false);
   }

   private void add(String name, String target) {
      this.bookmarks.addBookmark(name, target);
      this.listbox.add(name);
      this.listbox.makeVisible(this.listbox.getItemCount() - 1);
      this.listbox.select(this.listbox.getItemCount() - 1);
      this.select(true);
   }

   @Override
   public void dialogDone(Object who, boolean confirmed) {
   }
}