1
6 import java.awt.event.*;
7 import java.awt.*;
8 import java.awt.geom.*;
9 import javax.swing.*;
10 import java.io.*;
11
12 public abstract class LibraryItem implements Icon,ActionListener {
13
14 static Font font;
15
16 Icon icon = null;
17 Symbol iconsym = null;
18 File file;
19
20 static int DEFAULTHEIGHT = 30;
21 static int DEFAULTWIDTH = 30;
22
23 int height = DEFAULTHEIGHT;
24 int width = DEFAULTWIDTH;
25
26 int nameheight = 0;
27 int namewidth = 0;
28
29 public View view = null;
30
31 transient LibraryUser user;
32
33 public String name;
34
35 static String FONTFAMILY = "Utopia";
36 static int STYLE = Font.PLAIN;
37 static int FONTSIZE = 12;
38
39 static {
40 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
41 Font[] fontlist = ge.getAllFonts();
42 font = fontlist[0];
43 for (int i=0;i<fontlist.length;i++) {
44 if (fontlist[i].getFamily().compareTo(FONTFAMILY)==0) {
45 font = fontlist[i];
46 }
47 }
48 font = font.deriveFont(STYLE,FONTSIZE);
49 }
50
51 public String getName() {
52 return(file.getName());
53 }
54
55 public static LibraryItem getItem(File file,LibraryUser user) {
56 try {
57 if (file.isDirectory())
58 return(new Library(file,user));
59 String fullname = file.toString();
60 if (fullname.endsWith(".smb"))
61 return(new LibrarySymbol(file,user));
62 else if (fullname.endsWith(".itm")) {
63 try {
64 return((LibraryItem)(new ObjectInputStream(new FileInputStream(file))).readObject());
65 } catch (Exception e) {
66 panic(e);
67 return(null);
68 }
69 }
70 else throw new FileNotFoundException("Library must be directory or .smb or .itm");
71 } catch (Exception e) {panic(e);}
72 return(null);
73 }
74
75 public LibraryItem(File itemfile,LibraryUser user) {
76 this.user = user;
77 String fullname = itemfile.getName();
78 String basename = fullname;
79 String base = itemfile.getParent();
80 File iconname;
81
82 file = itemfile;
83
84 try {
85
86 if (fullname.endsWith(".smb"))
87 basename = fullname.substring(0,fullname.length()-4);
88 iconname = new File(base,basename.concat(".gif"));
89 if (iconname.exists()) {
90 icon = new ImageIcon(iconname.toURL());
91 height = icon.getIconHeight();
92 width = icon.getIconWidth();
93 }
94 else {
95 iconname = new File(base,basename.concat(".thm"));
96 if (iconname.exists())
97 iconsym = (Symbol)(new ObjectInputStream(new FileInputStream(iconname))).readObject();
98 setView();
99 }
100 } catch (Exception e) {panic(e);}
101 name = basename;
102
103 nameheight = 10;
104 namewidth = 30;
105 }
106
107 public int getIconHeight() {
108
109 return(height+nameheight);
110 }
111
112 public int getIconWidth() {
113
114
115
116 if (width>namewidth) return(width);
117 else return(namewidth);
118 }
119
120 public void paintIcon(Component c,Graphics g, int x, int y) {
121 g.setColor(Color.white);
122 Rectangle clip = g.getClipBounds();
123 g.fillRect(clip.x,clip.y,clip.width,clip.height);
124 g.setColor(Color.black);
125 if (icon!=null) icon.paintIcon(c,g,x,y);
126 else if (iconsym!=null) {
127
128 view.draw = (Graphics2D)g;
129 AffineTransform shift = AffineTransform.getTranslateInstance(x,y);
130 view.trans.preConcatenate(shift);
131
132 if (iconsym.stripSurveys())
133 iconsym.paint(view);
134 shift = AffineTransform.getTranslateInstance(-x,-y);
135 view.trans.preConcatenate(shift);
136 }
137 g.setFont(font);
138 g.drawString(name,x,y+height+nameheight);
139 }
140
141 public void setView() {
142 if (view==null && iconsym!=null) {
143 view = new View();
144 Rectangle2D bounds = iconsym.getBounds(view.trans);
145
146 view.mapscale.metersperpixel = CompPane.screenscale*10;
147
148 double size;
149 if (bounds.getHeight()/height>bounds.getWidth()/width) size = bounds.getHeight()/height;
150 size = bounds.getWidth()/width;
151
152 view.trans.preConcatenate(AffineTransform.getScaleInstance(1/size,1/size));
153
154 bounds = iconsym.getBounds(view.trans);
155 view.trans.preConcatenate(AffineTransform.getTranslateInstance(-bounds.getX(),-bounds.getY()));
156
157
158
159 }
160 }
161
162 static void panic(Exception e) {
163 ErrorLog.exception(e);
164 }
165 }
166