@@ -54,7 +54,7 @@ public class ThemeSelector extends JFrame implements Tool {
54
54
55
55
56
56
public String getMenuTitle () {
57
- return Language .text ("Theme Selector" );
57
+ return Language .text ("Theme Selector... " );
58
58
}
59
59
60
60
@@ -89,16 +89,40 @@ public void run() {
89
89
sketchbookFile = new File (Base .getSketchbookFolder (), "theme.txt" );
90
90
91
91
// figure out if the current theme in sketchbook is a known one
92
- checkCurrent ();
92
+ currentIndex = getCurrentIndex ();
93
93
94
94
setVisible (true );
95
95
}
96
96
97
97
98
- void setCurrent (int index ) {
98
+ private File nextBackupFile () {
99
+ int index = 0 ;
100
+ File backupFile ;
101
+ do {
102
+ index ++;
103
+ backupFile = new File (Base .getSketchbookFolder (), String .format ("theme.%03d" , index ));
104
+ } while (backupFile .exists ());
105
+ return backupFile ;
106
+ }
107
+
108
+
109
+ private void setCurrentIndex (int index ) {
99
110
//System.out.println("index is " + index);
100
111
currentIndex = index ;
101
112
try {
113
+ if (sketchbookFile .exists () && getCurrentIndex () == -1 ) {
114
+ // If the user has a custom theme they've modified,
115
+ // rename it to theme.001, theme.002, etc. as a backup
116
+ // to avoid overwriting anything they've created.
117
+ File backupFile = nextBackupFile ();
118
+ boolean success = sketchbookFile .renameTo (backupFile );
119
+ if (!success ) {
120
+ Messages .showWarning ("Could not back up theme" ,
121
+ "Could not save a backup of theme.txt in your sketchbook folder.\n " +
122
+ "Rename it manually and try setting the theme again." );
123
+ return ;
124
+ }
125
+ }
102
126
Util .saveFile (themeContents [index ], sketchbookFile );
103
127
Theme .load ();
104
128
@@ -113,8 +137,23 @@ void setCurrent(int index) {
113
137
}
114
138
115
139
116
- private void checkCurrent () {
117
- //
140
+ private int getCurrentIndex () {
141
+ try {
142
+ if (sketchbookFile .exists ()) {
143
+ String currentContents = Util .loadFile (sketchbookFile );
144
+ for (int i = 0 ; i < COUNT ; i ++) {
145
+ if (themeContents [i ].equals (currentContents )) {
146
+ return i ;
147
+ }
148
+ }
149
+ return -1 ;
150
+ }
151
+ return 0 ; // the default theme is index 0
152
+
153
+ } catch (Exception e ) {
154
+ e .printStackTrace ();
155
+ return -1 ; // could not identify the theme
156
+ }
118
157
}
119
158
120
159
@@ -123,9 +162,13 @@ private void checkCurrent() {
123
162
124
163
class ColorfulPanel extends JPanel {
125
164
static final int SCALE = 4 ;
126
- static final int EACH = 320 / SCALE ;
165
+ static final int DIM = 320 / SCALE ;
127
166
static final int BETWEEN = 100 / SCALE ;
128
- static final int SIZE = EACH *4 + BETWEEN *5 ;
167
+ static final int EACH = DIM + BETWEEN ;
168
+ static final int SIZE = DIM *4 + BETWEEN *5 ;
169
+
170
+ static final int OUTSET = 5 ;
171
+ static final int OUTLINE = 3 ;
129
172
130
173
Image image ;
131
174
@@ -136,11 +179,12 @@ class ColorfulPanel extends JPanel {
136
179
public void mousePressed (MouseEvent e ) {
137
180
//super.mousePressed(e);
138
181
139
- int col = constrain ((e .getX () - BETWEEN ) / ( EACH + BETWEEN ) );
140
- int row = constrain ((e .getY () - BETWEEN ) / ( EACH + BETWEEN ) );
182
+ int col = constrain ((e .getX () - BETWEEN ) / EACH );
183
+ int row = constrain ((e .getY () - BETWEEN ) / EACH );
141
184
int index = row *4 + col ;
142
185
143
- setCurrent (index );
186
+ setCurrentIndex (index );
187
+ repaint ();
144
188
}
145
189
});
146
190
}
@@ -152,6 +196,17 @@ private int constrain(int value) {
152
196
@ Override
153
197
public void paintComponent (Graphics g ) {
154
198
g .drawImage (image , 0 , 0 , SIZE , SIZE ,null );
199
+
200
+ Graphics2D g2 = (Graphics2D ) g ;
201
+ g2 .setStroke (new BasicStroke (OUTLINE ));
202
+ g2 .setColor (Color .GRAY );
203
+
204
+ int col = currentIndex % 4 ;
205
+ int row = currentIndex / 4 ;
206
+ g2 .drawRect (BETWEEN + EACH *col - OUTSET ,
207
+ BETWEEN + EACH *row - OUTSET ,
208
+ DIM + OUTSET *2 ,
209
+ DIM + OUTSET *2 );
155
210
}
156
211
157
212
@ Override
0 commit comments