summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/Library.java
blob: a531d71557d10aaff00e9b9e1462d77c9bc79d85 (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
package NET.worlds.scape;

import NET.worlds.network.URL;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

public class Library extends SuperRoot {
   private Object owningDialog;
   private Vector contents = new Vector();
   private LibEventHandler handler;
   protected String propertyName;
   private static Object classCookie = new Object();

   public static Library load(URL url) {
      return (Library)SuperRoot.readFile(url);
   }

   public Library(URL url, String name) {
      this.setSourceURL(url);
      this.setName(name);
   }

   public Library() {
   }

   public void save() {
      try {
         this.saveFile(this.getSourceURL());
      } catch (IOException var2) {
      }
   }

   public void delete() {
      new File(this.getSourceURL().unalias()).delete();
   }

   public void add(LibraryEntry ent) {
      assert ent.getOwner() == null;

      this.contents.addElement(ent);
      super.add(ent);
      this.changed();
   }

   public void delete(LibraryEntry ent) {
      boolean found = this.contents.removeElement(ent);

      assert found;

      assert ent.getOwner() == this;

      ent.detach();
      this.changed();
   }

   public void move(LibraryEntry src, LibraryEntry dst) {
      int oldPos = this.contents.indexOf(src);

      assert oldPos != -1;

      int newPos = this.contents.indexOf(dst);

      assert newPos != -1;

      this.contents.removeElement(src);
      this.contents.insertElementAt(src, newPos);
      this.changed();
   }

   void entryChanged(LibraryEntry which) {
      this.changed();
   }

   private void changed() {
      if (this.handler != null) {
         this.handler.libraryChanged(this);
      }
   }

   public Object getOwningDialog() {
      return this.owningDialog;
   }

   public Vector getContents() {
      return (Vector)this.contents.clone();
   }

   public String getPropertyName() {
      return this.propertyName;
   }

   public LibraryEntry getEntry(int index) {
      return (LibraryEntry)this.contents.elementAt(index);
   }

   public void setOwningDialog(Object o) {
      this.owningDialog = o;
   }

   @Override
   public void setName(String s) {
      super.setName(s);
      this.changed();
   }

   public void setPropertyName(String s) {
      this.propertyName = s;
      this.changed();
   }

   public void setEventHandler(LibEventHandler handler) {
      this.handler = handler;
   }

   @Override
   public Object properties(int index, int offset, int mode, Object value) throws NoSuchPropertyException {
      Object ret = null;
      switch (index - offset) {
         case 0:
            if (mode == 0) {
               ret = PropAdder.make(new VectorProperty(this, index, "Contents"));
            } else if (mode == 1) {
               ret = this.getContents();
            } else if (mode == 4) {
               this.delete((LibraryEntry)value);
            } else if (mode == 3) {
               this.add((LibraryEntry)value);
            } else if (mode == 5 && value instanceof LibraryEntry) {
               ret = value;
            }
            break;
         case 1:
            if (mode == 0) {
               ret = StringPropertyEditor.make(new Property(this, index, "Property Name").allowSetNull());
            } else if (mode == 1) {
               ret = this.getPropertyName();
            } else if (mode == 2) {
               this.setPropertyName((String)value);
            }
            break;
         default:
            ret = super.properties(index, offset + 2, mode, value);
      }

      return ret;
   }

   @Override
   public Object propertyParent() {
      return this.getOwningDialog();
   }

   @Override
   public void saveState(Saver s) throws IOException {
      s.saveVersion(2, classCookie);
      super.saveState(s);
      s.saveVector(this.contents);
      s.saveString(this.propertyName);
   }

   @Override
   public void restoreState(Restorer r) throws IOException, TooNewException {
      Vector newContents = null;
      switch (r.restoreVersion(classCookie)) {
         case 0:
            this.setName(r.restoreString());
            r.restoreString();
            r.restoreString();
            newContents = r.restoreVector();
            break;
         case 1:
            super.restoreState(r);
            r.restoreString();
            r.restoreString();
            newContents = r.restoreVector();
            break;
         case 2:
            super.restoreState(r);
            newContents = r.restoreVector();
            this.propertyName = r.restoreString();
            break;
         default:
            throw new TooNewException();
      }

      for (int i = 0; i < newContents.size(); i++) {
         this.add((LibraryEntry)newContents.elementAt(i));
      }
   }
}