-
-
Notifications
You must be signed in to change notification settings - Fork 256
/
IJThemesPanel.java
526 lines (448 loc) · 16.6 KB
/
IJThemesPanel.java
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
* Copyright 2019 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formdev.flatlaf.demo.intellijthemes;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.event.*;
import com.formdev.flatlaf.FlatDarculaLaf;
import com.formdev.flatlaf.FlatDarkLaf;
import com.formdev.flatlaf.FlatIntelliJLaf;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.FlatLightLaf;
import com.formdev.flatlaf.FlatPropertiesLaf;
import com.formdev.flatlaf.IntelliJTheme;
import com.formdev.flatlaf.demo.DemoPrefs;
import com.formdev.flatlaf.extras.FlatAnimatedLafChange;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import com.formdev.flatlaf.ui.FlatListUI;
import com.formdev.flatlaf.util.LoggingFacade;
import com.formdev.flatlaf.util.StringUtils;
import net.miginfocom.swing.*;
/**
* @author Karl Tauber
*/
public class IJThemesPanel
extends JPanel
{
public static final String THEMES_PACKAGE = "/com/formdev/flatlaf/intellijthemes/themes/";
private final IJThemesManager themesManager = new IJThemesManager();
private final List<IJThemeInfo> themes = new ArrayList<>();
private final HashMap<Integer, String> categories = new HashMap<>();
private final PropertyChangeListener lafListener = this::lafChanged;
private final WindowListener windowListener = new WindowAdapter() {
@Override
public void windowActivated( WindowEvent e ) {
IJThemesPanel.this.windowActivated();
}
};
private Window window;
private File lastDirectory;
private boolean isAdjustingThemesList;
public IJThemesPanel() {
initComponents();
saveButton.setEnabled( false );
sourceCodeButton.setEnabled( false );
saveButton.setIcon( new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/download.svg" ) );
sourceCodeButton.setIcon( new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/github.svg" ) );
// create renderer
themesList.setCellRenderer( new DefaultListCellRenderer() {
private int index;
private boolean isSelected;
private int titleHeight;
@Override
public Component getListCellRendererComponent( JList<?> list, Object value,
int index, boolean isSelected, boolean cellHasFocus )
{
this.index = index;
this.isSelected = isSelected;
this.titleHeight = 0;
String title = categories.get( index );
String name = ((IJThemeInfo)value).name;
int sep = name.indexOf( '/' );
if( sep >= 0 )
name = name.substring( sep + 1 ).trim();
JComponent c = (JComponent) super.getListCellRendererComponent( list, name, index, isSelected, cellHasFocus );
c.setToolTipText( buildToolTip( (IJThemeInfo) value ) );
if( title != null ) {
Border titledBorder = new ListCellTitledBorder( themesList, title );
c.setBorder( new CompoundBorder( titledBorder, c.getBorder() ) );
titleHeight = titledBorder.getBorderInsets( c ).top;
}
return c;
}
@Override
public boolean isOpaque() {
return !isSelectedTitle();
}
@Override
protected void paintComponent( Graphics g ) {
if( isSelectedTitle() ) {
g.setColor( getBackground() );
FlatListUI.paintCellSelection( themesList, g, index, 0, titleHeight, getWidth(), getHeight() - titleHeight );
}
super.paintComponent( g );
}
private boolean isSelectedTitle() {
return titleHeight > 0 && isSelected && UIManager.getLookAndFeel() instanceof FlatLaf;
}
private String buildToolTip( IJThemeInfo ti ) {
if( ti.themeFile != null )
return ti.themeFile.getPath();
if( ti.resourceName == null )
return ti.name;
return "Name: " + ti.name
+ "\nLicense: " + ti.license
+ "\nSource Code: " + ti.sourceCodeUrl;
}
} );
updateThemesList();
}
private void updateThemesList() {
int filterLightDark = filterComboBox.getSelectedIndex();
boolean showLight = (filterLightDark != 2);
boolean showDark = (filterLightDark != 1);
// load theme infos
themesManager.loadBundledThemes();
themesManager.loadThemesFromDirectory();
// sort themes by name
Comparator<? super IJThemeInfo> comparator = (t1, t2) -> t1.name.compareToIgnoreCase( t2.name );
themesManager.bundledThemes.sort( comparator );
themesManager.moreThemes.sort( comparator );
// remember selection (must be invoked before clearing themes field)
IJThemeInfo oldSel = themesList.getSelectedValue();
themes.clear();
categories.clear();
// add core themes at beginning
categories.put( themes.size(), "Core Themes" );
if( showLight )
themes.add( new IJThemeInfo( "FlatLaf Light", null, false, null, null, null, null, null, FlatLightLaf.class.getName() ) );
if( showDark )
themes.add( new IJThemeInfo( "FlatLaf Dark", null, true, null, null, null, null, null, FlatDarkLaf.class.getName() ) );
if( showLight )
themes.add( new IJThemeInfo( "FlatLaf IntelliJ", null, false, null, null, null, null, null, FlatIntelliJLaf.class.getName() ) );
if( showDark )
themes.add( new IJThemeInfo( "FlatLaf Darcula", null, true, null, null, null, null, null, FlatDarculaLaf.class.getName() ) );
// add themes from directory
categories.put( themes.size(), "Current Directory" );
themes.addAll( themesManager.moreThemes );
// add uncategorized bundled themes
categories.put( themes.size(), "IntelliJ Themes" );
for( IJThemeInfo ti : themesManager.bundledThemes ) {
boolean show = (showLight && !ti.dark) || (showDark && ti.dark);
if( show && !ti.name.contains( "/" ) )
themes.add( ti );
}
// add categorized bundled themes
String lastCategory = null;
for( IJThemeInfo ti : themesManager.bundledThemes ) {
boolean show = (showLight && !ti.dark) || (showDark && ti.dark);
int sep = ti.name.indexOf( '/' );
if( !show || sep < 0 )
continue;
String category = ti.name.substring( 0, sep ).trim();
if( !Objects.equals( lastCategory, category ) ) {
lastCategory = category;
categories.put( themes.size(), category );
}
themes.add( ti );
}
// fill themes list
themesList.setModel( new AbstractListModel<IJThemeInfo>() {
@Override
public int getSize() {
return themes.size();
}
@Override
public IJThemeInfo getElementAt( int index ) {
return themes.get( index );
}
} );
// restore selection
if( oldSel != null ) {
for( int i = 0; i < themes.size(); i++ ) {
IJThemeInfo theme = themes.get( i );
if( oldSel.name.equals( theme.name ) &&
Objects.equals( oldSel.resourceName, theme.resourceName ) &&
Objects.equals( oldSel.themeFile, theme.themeFile ) &&
Objects.equals( oldSel.lafClassName, theme.lafClassName ) )
{
themesList.setSelectedIndex( i );
break;
}
}
// select first theme if none selected
if( themesList.getSelectedIndex() < 0 )
themesList.setSelectedIndex( 0 );
}
// scroll selection into visible area
int sel = themesList.getSelectedIndex();
if( sel >= 0 ) {
Rectangle bounds = themesList.getCellBounds( sel, sel );
if( bounds != null )
themesList.scrollRectToVisible( bounds );
}
}
public void selectPreviousTheme() {
int sel = themesList.getSelectedIndex();
if( sel > 0 )
themesList.setSelectedIndex( sel - 1 );
}
public void selectNextTheme() {
int sel = themesList.getSelectedIndex();
themesList.setSelectedIndex( sel + 1 );
}
private void themesListValueChanged( ListSelectionEvent e ) {
IJThemeInfo themeInfo = themesList.getSelectedValue();
boolean bundledTheme = (themeInfo != null && themeInfo.resourceName != null);
saveButton.setEnabled( bundledTheme );
sourceCodeButton.setEnabled( bundledTheme );
if( e.getValueIsAdjusting() || isAdjustingThemesList )
return;
EventQueue.invokeLater( () -> {
setTheme( themeInfo );
} );
}
private void setTheme( IJThemeInfo themeInfo ) {
if( themeInfo == null )
return;
// change look and feel
if( themeInfo.lafClassName != null ) {
if( themeInfo.lafClassName.equals( UIManager.getLookAndFeel().getClass().getName() ) )
return;
FlatAnimatedLafChange.showSnapshot();
try {
UIManager.setLookAndFeel( themeInfo.lafClassName );
} catch( Exception ex ) {
LoggingFacade.INSTANCE.logSevere( null, ex );
showInformationDialog( "Failed to create '" + themeInfo.lafClassName + "'.", ex );
}
} else if( themeInfo.themeFile != null ) {
FlatAnimatedLafChange.showSnapshot();
try {
if( themeInfo.themeFile.getName().endsWith( ".properties" ) ) {
FlatLaf.setup( new FlatPropertiesLaf( themeInfo.name, themeInfo.themeFile ) );
} else
FlatLaf.setup( IntelliJTheme.createLaf( new FileInputStream( themeInfo.themeFile ) ) );
DemoPrefs.getState().put( DemoPrefs.KEY_LAF_THEME, DemoPrefs.FILE_PREFIX + themeInfo.themeFile );
} catch( Exception ex ) {
LoggingFacade.INSTANCE.logSevere( null, ex );
showInformationDialog( "Failed to load '" + themeInfo.themeFile + "'.", ex );
}
} else {
FlatAnimatedLafChange.showSnapshot();
IntelliJTheme.setup( getClass().getResourceAsStream( THEMES_PACKAGE + themeInfo.resourceName ) );
DemoPrefs.getState().put( DemoPrefs.KEY_LAF_THEME, DemoPrefs.RESOURCE_PREFIX + themeInfo.resourceName );
}
// update all components
FlatLaf.updateUI();
FlatAnimatedLafChange.hideSnapshotWithAnimation();
}
private void saveTheme() {
IJThemeInfo themeInfo = themesList.getSelectedValue();
if( themeInfo == null || themeInfo.resourceName == null )
return;
JFileChooser fileChooser = new JFileChooser();
fileChooser.setSelectedFile( new File( lastDirectory, themeInfo.resourceName ) );
if( fileChooser.showSaveDialog( SwingUtilities.windowForComponent( this ) ) != JFileChooser.APPROVE_OPTION )
return;
File file = fileChooser.getSelectedFile();
lastDirectory = file.getParentFile();
// save theme
try {
Files.copy( getClass().getResourceAsStream( THEMES_PACKAGE + themeInfo.resourceName ),
file.toPath(), StandardCopyOption.REPLACE_EXISTING );
} catch( IOException ex ) {
showInformationDialog( "Failed to save theme to '" + file + "'.", ex );
return;
}
// save license
if( themeInfo.licenseFile != null ) {
try {
File licenseFile = new File( file.getParentFile(),
StringUtils.removeTrailing( file.getName(), ".theme.json" ) +
themeInfo.licenseFile.substring( themeInfo.licenseFile.indexOf( '.' ) ) );
Files.copy( getClass().getResourceAsStream( THEMES_PACKAGE + themeInfo.licenseFile ),
licenseFile.toPath(), StandardCopyOption.REPLACE_EXISTING );
} catch( IOException ex ) {
showInformationDialog( "Failed to save theme license to '" + file + "'.", ex );
return;
}
}
}
private void browseSourceCode() {
IJThemeInfo themeInfo = themesList.getSelectedValue();
if( themeInfo == null || themeInfo.resourceName == null )
return;
String themeUrl = (themeInfo.sourceCodeUrl + '/' + themeInfo.sourceCodePath).replace( " ", "%20" );
try {
Desktop.getDesktop().browse( new URI( themeUrl ) );
} catch( IOException | URISyntaxException ex ) {
showInformationDialog( "Failed to browse '" + themeUrl + "'.", ex );
}
}
private void showInformationDialog( String message, Exception ex ) {
JOptionPane.showMessageDialog( SwingUtilities.windowForComponent( this ),
message + "\n\n" + ex.getMessage(),
"FlatLaf", JOptionPane.INFORMATION_MESSAGE );
}
@Override
public void addNotify() {
super.addNotify();
selectedCurrentLookAndFeel();
UIManager.addPropertyChangeListener( lafListener );
window = SwingUtilities.windowForComponent( this );
if( window != null )
window.addWindowListener( windowListener );
}
@Override
public void removeNotify() {
super.removeNotify();
UIManager.removePropertyChangeListener( lafListener );
if( window != null ) {
window.removeWindowListener( windowListener );
window = null;
}
}
private void lafChanged( PropertyChangeEvent e ) {
if( "lookAndFeel".equals( e.getPropertyName() ) )
selectedCurrentLookAndFeel();
}
private void windowActivated() {
// refresh themes list on window activation
if( themesManager.hasThemesFromDirectoryChanged() )
updateThemesList();
}
private void selectedCurrentLookAndFeel() {
LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
String theme = UIManager.getLookAndFeelDefaults().getString( DemoPrefs.THEME_UI_KEY );
if( theme == null && (lookAndFeel instanceof IntelliJTheme.ThemeLaf || lookAndFeel instanceof FlatPropertiesLaf) )
return;
Predicate<IJThemeInfo> test;
if( theme != null && theme.startsWith( DemoPrefs.RESOURCE_PREFIX ) ) {
String resourceName = theme.substring( DemoPrefs.RESOURCE_PREFIX.length() );
test = ti -> Objects.equals( ti.resourceName, resourceName );
} else if( theme != null && theme.startsWith( DemoPrefs.FILE_PREFIX ) ) {
File themeFile = new File( theme.substring( DemoPrefs.FILE_PREFIX.length() ) );
test = ti -> Objects.equals( ti.themeFile, themeFile );
} else {
String lafClassName = lookAndFeel.getClass().getName();
test = ti -> Objects.equals( ti.lafClassName, lafClassName );
}
int newSel = -1;
for( int i = 0; i < themes.size(); i++ ) {
if( test.test( themes.get( i ) ) ) {
newSel = i;
break;
}
}
isAdjustingThemesList = true;
if( newSel >= 0 ) {
if( newSel != themesList.getSelectedIndex() )
themesList.setSelectedIndex( newSel );
} else
themesList.clearSelection();
isAdjustingThemesList = false;
}
private void filterChanged() {
updateThemesList();
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
JLabel themesLabel = new JLabel();
toolBar = new JToolBar();
saveButton = new JButton();
sourceCodeButton = new JButton();
filterComboBox = new JComboBox<>();
themesScrollPane = new JScrollPane();
themesList = new JList<>();
//======== this ========
setLayout(new MigLayout(
"insets dialog,hidemode 3",
// columns
"[grow,fill]",
// rows
"[]3" +
"[grow,fill]"));
//---- themesLabel ----
themesLabel.setText("Themes:");
add(themesLabel, "cell 0 0");
//======== toolBar ========
{
toolBar.setFloatable(false);
//---- saveButton ----
saveButton.setToolTipText("Save .theme.json of selected IntelliJ theme to file.");
saveButton.addActionListener(e -> saveTheme());
toolBar.add(saveButton);
//---- sourceCodeButton ----
sourceCodeButton.setToolTipText("Opens the source code repository of selected IntelliJ theme in the browser.");
sourceCodeButton.addActionListener(e -> browseSourceCode());
toolBar.add(sourceCodeButton);
}
add(toolBar, "cell 0 0,alignx right,growx 0");
//---- filterComboBox ----
filterComboBox.setModel(new DefaultComboBoxModel<>(new String[] {
"all",
"light",
"dark"
}));
filterComboBox.putClientProperty("JComponent.minimumWidth", 0);
filterComboBox.setFocusable(false);
filterComboBox.addActionListener(e -> filterChanged());
add(filterComboBox, "cell 0 0,alignx right,growx 0");
//======== themesScrollPane ========
{
//---- themesList ----
themesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
themesList.addListSelectionListener(e -> themesListValueChanged(e));
themesScrollPane.setViewportView(themesList);
}
add(themesScrollPane, "cell 0 1");
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JToolBar toolBar;
private JButton saveButton;
private JButton sourceCodeButton;
private JComboBox<String> filterComboBox;
private JScrollPane themesScrollPane;
private JList<IJThemeInfo> themesList;
// JFormDesigner - End of variables declaration //GEN-END:variables
}