@@ -54,7 +54,7 @@ public class ThemeSelector extends JFrame implements Tool {
5454
5555
5656 public String getMenuTitle () {
57- return Language .text ("Theme Selector" );
57+ return Language .text ("Theme Selector... " );
5858 }
5959
6060
@@ -89,16 +89,40 @@ public void run() {
8989 sketchbookFile = new File (Base .getSketchbookFolder (), "theme.txt" );
9090
9191 // figure out if the current theme in sketchbook is a known one
92- checkCurrent ();
92+ currentIndex = getCurrentIndex ();
9393
9494 setVisible (true );
9595 }
9696
9797
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 ) {
99110 //System.out.println("index is " + index);
100111 currentIndex = index ;
101112 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+ }
102126 Util .saveFile (themeContents [index ], sketchbookFile );
103127 Theme .load ();
104128
@@ -113,8 +137,23 @@ void setCurrent(int index) {
113137 }
114138
115139
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+ }
118157 }
119158
120159
@@ -123,9 +162,13 @@ private void checkCurrent() {
123162
124163 class ColorfulPanel extends JPanel {
125164 static final int SCALE = 4 ;
126- static final int EACH = 320 / SCALE ;
165+ static final int DIM = 320 / SCALE ;
127166 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 ;
129172
130173 Image image ;
131174
@@ -136,11 +179,12 @@ class ColorfulPanel extends JPanel {
136179 public void mousePressed (MouseEvent e ) {
137180 //super.mousePressed(e);
138181
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 );
141184 int index = row *4 + col ;
142185
143- setCurrent (index );
186+ setCurrentIndex (index );
187+ repaint ();
144188 }
145189 });
146190 }
@@ -152,6 +196,17 @@ private int constrain(int value) {
152196 @ Override
153197 public void paintComponent (Graphics g ) {
154198 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 );
155210 }
156211
157212 @ Override
0 commit comments