Skip to content
This repository was archived by the owner on Apr 15, 2023. It is now read-only.

Commit eb0590b

Browse files
Snippet opened in JFrame and remember window size
1 parent 8233e50 commit eb0590b

File tree

8 files changed

+95
-26
lines changed

8 files changed

+95
-26
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<properties>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15-
<tagmycode-sdk.version>1.3.1</tagmycode-sdk.version>
15+
<tagmycode-sdk.version>1.3.2-SNAPSHOT</tagmycode-sdk.version>
1616
<buildDate>${maven.build.timestamp}</buildDate>
1717
<maven.build.timestamp.format>yyyy-MM-dd</maven.build.timestamp.format>
1818
</properties>

src/main/java/com/tagmycode/plugin/Framework.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.tagmycode.plugin.exception.TagMyCodeStorageException;
66
import com.tagmycode.plugin.gui.IDocumentInsertText;
77
import com.tagmycode.plugin.gui.IOnErrorCallback;
8+
import com.tagmycode.plugin.gui.WindowType;
89
import com.tagmycode.plugin.gui.form.*;
910
import com.tagmycode.sdk.Client;
1011
import com.tagmycode.sdk.SaveFilePath;
@@ -25,6 +26,7 @@
2526

2627
public class Framework implements IOnErrorCallback {
2728
public final static Logger LOGGER = Logger.getLogger(Framework.class);
29+
public static final String USER_SETTINGS_PROPERTIES = "user_settings.properties";
2830

2931
private final TagMyCode tagMyCode;
3032
private final Frame parentFrame;
@@ -37,21 +39,21 @@ public class Framework implements IOnErrorCallback {
3739
private MainWindow mainWindow;
3840
private QuickSearchDialog quickSearchDialog;
3941
private AboutDialog aboutDialog;
40-
private SnippetDialog snippetDialog;
4142
private final CrashService crashService;
4243
private final SyntaxSnippetEditorFactory syntaxSnippetEditorFactory;
4344
private UserPreferences userPreferences;
4445
private final SaveFilePath saveFilePath;
4546

4647
public Framework(TagMyCodeApi tagMyCodeApi, FrameworkConfig frameworkConfig, AbstractSecret secret) throws SQLException {
48+
WindowType.setDefaultType(WindowType.Type.JDIALOG);
4749
saveFilePath = frameworkConfig.getSaveFilePath();
4850
this.browser = frameworkConfig.getBrowser();
4951
Client client = new Client(tagMyCodeApi, secret.getConsumerId(), secret.getConsumerSecret(), new Wallet(frameworkConfig.getPasswordKeyChain()));
5052
tagMyCode = new TagMyCode(client);
5153
this.messageManager = frameworkConfig.getMessageManager();
5254
this.parentFrame = frameworkConfig.getParentFrame();
5355
this.taskFactory = frameworkConfig.getTask();
54-
userPreferences = new UserPreferences(new File(saveFilePath.getPathWith("user_settings.properties")));
56+
userPreferences = new UserPreferences(new File(saveFilePath.getPathWith(USER_SETTINGS_PROPERTIES)));
5557
userPreferences.load();
5658
StorageEngine storageEngine = new StorageEngine(frameworkConfig.getDbService());
5759
this.data = new Data(storageEngine);
@@ -60,7 +62,6 @@ public Framework(TagMyCodeApi tagMyCodeApi, FrameworkConfig frameworkConfig, Abs
6062
pollingProcess = new SnippetsUpdatePollingProcess(this);
6163
version = frameworkConfig.getVersionObject();
6264
aboutDialog = new AboutDialog(this, getParentFrame());
63-
snippetDialog = new SnippetDialog(this, getParentFrame());
6465
this.mainWindow = new MainWindow(this);
6566
crashService = new CrashService(new CrashClient(client), data, tagMyCode, version, secret.getConsumerId());
6667
}
@@ -100,7 +101,7 @@ public LoginDialog showLoginDialog() {
100101

101102
public SnippetDialog showSnippetDialog(Snippet snippet) {
102103
// TODO check if can operate
103-
104+
SnippetDialog snippetDialog = new SnippetDialog(this, getParentFrame());
104105
snippetDialog.setSnippet(snippet);
105106
snippetDialog.display();
106107
return snippetDialog;

src/main/java/com/tagmycode/plugin/UserPreferences.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
public class UserPreferences {
77
public static final String TOGGLE_FILTER_BUTTON_SELECTED = "toggle_filter_button_selected";
8+
public static final String SNIPPET_DIALOG_HEIGHT = "snippet_dialog_height";
9+
public static final String SNIPPET_DIALOG_WIDTH = "snippet_dialog_width";
810
private final Properties properties;
911
private final File propertyFile;
1012

@@ -25,6 +27,18 @@ public void setBoolean(String key, boolean value) {
2527
properties.setProperty(key, String.valueOf(value));
2628
}
2729

30+
public Integer getInteger(String key, Integer defaultValue) {
31+
String property = properties.getProperty(key);
32+
if (property == null) {
33+
return defaultValue;
34+
}
35+
return Integer.parseInt(property);
36+
}
37+
38+
public void setInteger(String key, int value) {
39+
properties.setProperty(key, String.valueOf(value));
40+
}
41+
2842
public void store() throws IOException {
2943
OutputStream out = new FileOutputStream(propertyFile);
3044
properties.store(out, this.getClass().toString());
@@ -33,8 +47,7 @@ public void store() throws IOException {
3347
public void load() {
3448
try {
3549
properties.load(new FileInputStream(propertyFile));
36-
} catch (IOException e) {
37-
e.printStackTrace();
50+
} catch (IOException ignored) {
3851
}
3952
}
4053

src/main/java/com/tagmycode/plugin/gui/CenterLocation.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
import java.awt.*;
44

55
public class CenterLocation {
6-
private static CenterLocationType centerType = CenterLocationType.CENTER_FRAME;
7-
private int x;
8-
private int y;
6+
private final int x;
7+
private final int y;
98

109
public CenterLocation(Frame frame, Window window) {
10+
this(frame, window, CenterLocationType.CENTER_FRAME);
11+
}
12+
13+
public CenterLocation(Frame frame, Window window, CenterLocationType centerLocationType) {
1114
int width;
1215
int height;
1316
int offsetX = 0;
1417
int offsetY = 0;
1518

16-
if (frame == null || centerType == CenterLocationType.CENTER_SCREEN) {
19+
if (frame == null || centerLocationType == CenterLocationType.CENTER_SCREEN) {
1720
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
1821
width = (int) screenSize.getWidth();
1922
height = (int) screenSize.getHeight();
@@ -27,10 +30,6 @@ public CenterLocation(Frame frame, Window window) {
2730
y = offsetY + (height / 2) - (window.getHeight() / 2);
2831
}
2932

30-
public static void setCenterType(CenterLocationType centerType) {
31-
CenterLocation.centerType = centerType;
32-
}
33-
3433
public int getX() {
3534
return x;
3635
}

src/main/java/com/tagmycode/plugin/gui/form/SnippetDialog.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
import com.intellij.uiDesigner.core.Spacer;
77
import com.tagmycode.plugin.Framework;
88
import com.tagmycode.plugin.GuiThread;
9+
import com.tagmycode.plugin.UserPreferences;
910
import com.tagmycode.plugin.exception.TagMyCodeStorageException;
1011
import com.tagmycode.plugin.gui.SyntaxSnippetEditor;
12+
import com.tagmycode.plugin.gui.WindowType;
1113
import com.tagmycode.plugin.gui.field.AbstractFieldValidation;
1214
import com.tagmycode.plugin.gui.field.CodeFieldValidation;
1315
import com.tagmycode.plugin.gui.field.TitleFieldValidation;
1416
import com.tagmycode.plugin.operation.EditSnippetOperation;
17+
import com.tagmycode.sdk.model.DefaultSnippet;
1518
import com.tagmycode.sdk.model.Language;
1619
import com.tagmycode.sdk.model.Snippet;
1720

@@ -21,6 +24,8 @@
2124
import java.awt.*;
2225
import java.awt.event.ActionEvent;
2326
import java.awt.event.ActionListener;
27+
import java.awt.event.ComponentAdapter;
28+
import java.awt.event.ComponentEvent;
2429
import java.util.ArrayList;
2530
import java.util.Date;
2631

@@ -29,6 +34,7 @@
2934
public class SnippetDialog extends Windowable {
3035
private static final String NEW_SNIPPET_TITLE = "New snippet";
3136
private static final String EDIT_SNIPPET_TITLE = "Edit snippet";
37+
3238
private final SyntaxSnippetEditor codeEditorPane;
3339

3440
private JPanel contentPane;
@@ -46,21 +52,27 @@ public class SnippetDialog extends Windowable {
4652
private DefaultComboBoxModel<Language> defaultComboBoxModel;
4753
private EditSnippetOperation createAndEditSnippetOperation;
4854
private boolean isModified = false;
49-
private Snippet currentSnippet;
55+
private Snippet currentSnippet = new DefaultSnippet();
5056

5157
public SnippetDialog(final Framework framework, Frame parent) {
52-
super(framework, parent);
58+
super(framework, parent, WindowType.Type.JFRAME);
5359
codeEditorPane = framework.getSyntaxSnippetEditorFactory().create();
5460
snippetPane.add(codeEditorPane.getMainComponent());
61+
5562
defaultInitWindow();
5663
initWindow();
5764
}
5865

5966
public void setSnippet(Snippet snippet) {
60-
setTitle(isStoredSnippet(snippet) ? EDIT_SNIPPET_TITLE : NEW_SNIPPET_TITLE);
6167
currentSnippet = snippet;
6268
populateFieldsWithSnippet(snippet);
6369
snippetMarkedAsSaved();
70+
updateWindowTitle();
71+
}
72+
73+
protected void updateWindowTitle() {
74+
String asterisk = isModified ? "*" : "";
75+
setTitle(isStoredSnippet(currentSnippet) ? asterisk + titleBox.getText() : NEW_SNIPPET_TITLE);
6476
}
6577

6678
protected void populateFieldsWithSnippet(Snippet snippet) {
@@ -85,13 +97,24 @@ protected void initWindow() {
8597
setPlaceholder("Description", descriptionTextField);
8698
setPlaceholder("tags space separated", tagsTextField);
8799

88-
setSize(650, 450);
100+
int width = framework.getUserPreferences().getInteger(UserPreferences.SNIPPET_DIALOG_WIDTH, 650);
101+
int height = framework.getUserPreferences().getInteger(UserPreferences.SNIPPET_DIALOG_HEIGHT, 450);
102+
setSize(width, height);
89103
setResizable(true);
90104
setModal(false);
91105

92106
populateLanguages();
93107
restorePreferences();
94108

109+
getWindow().addComponentListener(new ComponentAdapter() {
110+
public void componentResized(ComponentEvent e) {
111+
Component source = (Component) e.getSource();
112+
Dimension size = source.getSize();
113+
framework.getUserPreferences().setInteger(UserPreferences.SNIPPET_DIALOG_WIDTH, (int) size.getWidth());
114+
framework.getUserPreferences().setInteger(UserPreferences.SNIPPET_DIALOG_HEIGHT, (int) size.getHeight());
115+
}
116+
});
117+
95118
new GuiThread().execute(new Runnable() {
96119
@Override
97120
public void run() {
@@ -247,7 +270,7 @@ private boolean isStoredSnippet(Snippet currentSnippet) {
247270
return currentSnippet != null && currentSnippet.getLocalId() != 0;
248271
}
249272

250-
public JComboBox getLanguageComboBox() {
273+
public JComboBox<Language> getLanguageComboBox() {
251274
return languageComboBox;
252275
}
253276

@@ -303,6 +326,7 @@ public void snippetMarkedAsSaved() {
303326

304327
public void snippetMarkedAsModified() {
305328
this.isModified = true;
329+
this.updateWindowTitle();
306330
}
307331

308332
public EditSnippetOperation getCreateAndEditSnippetOperation() {

src/main/java/com/tagmycode/plugin/gui/form/Windowable.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
public abstract class Windowable extends AbstractGui implements IOnErrorCallback {
1313

1414
private Window window;
15-
private WindowType.Type type;
15+
private WindowType.Type windowType;
1616
private Frame parentFrame;
1717
protected Framework framework;
1818
private String title;
1919

20-
public Windowable(Framework framework, Frame parent) {
20+
public Windowable(Framework framework, Frame parent, WindowType.Type windowType) {
2121
this.framework = framework;
22-
this.type = WindowType.getDefaultType();
22+
this.windowType = windowType;
2323
parentFrame = parent;
2424
if (isJDialog()) {
2525
window = new JDialog(parentFrame, true);
@@ -30,6 +30,9 @@ public Windowable(Framework framework, Frame parent) {
3030
}
3131
}
3232

33+
public Windowable(Framework framework, Frame parent) {
34+
this(framework, parent, WindowType.getDefaultType());
35+
}
3336

3437
protected abstract void initWindow();
3538

@@ -80,7 +83,7 @@ public void display() {
8083

8184
@Override
8285
public void run() {
83-
CenterLocation location = new CenterLocation(parentFrame, window);
86+
CenterLocation location = new CenterLocation(parentFrame, window, CenterLocationType.CENTER_FRAME);
8487
window.setLocation(location.getX(), location.getY());
8588
window.setVisible(true);
8689
window.revalidate();
@@ -89,6 +92,10 @@ public void run() {
8992
});
9093
}
9194

95+
public Component getWindow() {
96+
return window;
97+
}
98+
9299
public void setSize(int i, int i1) {
93100
window.setSize(i, i1);
94101
}
@@ -160,7 +167,7 @@ public void hideDialog() {
160167
}
161168

162169
private boolean isJDialog() {
163-
return type == WindowType.Type.JDIALOG;
170+
return windowType == WindowType.Type.JDIALOG;
164171
}
165172

166173
private JDialog getJDialog() {

src/test/java/com/tagmycode/plugin/UserPreferencesTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,14 @@ public void booleanValue() {
1818
assertTrue(userPreferences.getBoolean("key", false));
1919
}
2020

21+
@Test
22+
public void intValue() {
23+
UserPreferences userPreferences = new UserPreferences(new File(""));
24+
assertEquals(Integer.valueOf(5), userPreferences.getInteger("not_existing_key", 5));
25+
assertNull(userPreferences.getInteger("not_existing_key", null));
26+
27+
userPreferences.setInteger("key", 5);
28+
assertEquals(Integer.valueOf(5), userPreferences.getInteger("key", 1));
29+
}
30+
2131
}

src/test/java/com/tagmycode/plugin/gui/form/SnippetDialogTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,22 @@ public void testCreateSnippetObjectForSnippet() throws Exception {
7171
assertNotNull(snippetObject.getCreationDate());
7272
assertNotNull(snippetObject.getUpdateDate());
7373

74-
assertEquals("Edit snippet", snippetDialog.getTitle());
74+
assertEquals("My title", snippetDialog.getTitle());
75+
}
76+
77+
@Test
78+
public void prependAsteriskOnWindowTitleIfItsModified() throws Exception {
79+
SnippetDialog snippetDialog = createSnippetDialog(createFramework());
80+
Snippet snippet = resourceGenerate.aSnippet().setLocalId(100).setId(0);
81+
snippetDialog.setSnippet(snippet);
82+
83+
assertEquals("My title", snippetDialog.getTitle());
84+
assertFalse(snippetDialog.isModified());
85+
86+
snippetDialog.getTitleBox().setText(snippetDialog.getTitleBox().getText() + " word");
87+
88+
assertEquals("*My title word", snippetDialog.getTitle());
89+
assertTrue(snippetDialog.isModified());
7590
}
7691

7792
@Test

0 commit comments

Comments
 (0)