Skip to content

Commit b7d206f

Browse files
committed
Add the last 5 selected encodings to top of the encoding list by default
1 parent da23ad5 commit b7d206f

3 files changed

Lines changed: 125 additions & 2 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7+
* Other names may be trademarks of their respective owners.
8+
*
9+
* The contents of this file are subject to the terms of either the GNU
10+
* General Public License Version 2 only ("GPL") or the Common
11+
* Development and Distribution License("CDDL") (collectively, the
12+
* "License"). You may not use this file except in compliance with the
13+
* License. You can obtain a copy of the License at
14+
* http://www.netbeans.org/cddl-gplv2.html
15+
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16+
* specific language governing permissions and limitations under the
17+
* License. When distributing the software, include this License Header
18+
* Notice in each file and include the License file at
19+
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20+
* particular file as subject to the "Classpath" exception as provided
21+
* by Oracle in the GPL Version 2 section of the License file that
22+
* accompanied this code. If applicable, add the following below the
23+
* License Header, with the fields enclosed by brackets [] replaced by
24+
* your own identifying information:
25+
* "Portions Copyrighted [year] [name of copyright owner]"
26+
*
27+
* If you wish your version of this file to be governed by only the CDDL
28+
* or only the GPL Version 2, indicate your decision by adding
29+
* "[Contributor] elects to include this software in this distribution
30+
* under the [CDDL or GPL Version 2] license." If you do not indicate a
31+
* single choice of license, a recipient has the option to distribute
32+
* your version of this file under either the CDDL, the GPL Version 2 or
33+
* to extend the choice of license to its licensees as provided above.
34+
* However, if you add GPL Version 2 code and therefore, elected the GPL
35+
* Version 2 license, then the option applies only if the new code is
36+
* made subject to such option by the copyright holder.
37+
*
38+
* Contributor(s):
39+
*/
40+
package com.junichi11.netbeans.modules.encoding.options;
41+
42+
import java.util.ArrayList;
43+
import java.util.Arrays;
44+
import java.util.Collections;
45+
import java.util.List;
46+
import java.util.prefs.Preferences;
47+
import org.openide.util.NbPreferences;
48+
49+
/**
50+
*
51+
* @author junichi11
52+
*/
53+
public final class EncodingOptions {
54+
55+
private static final int LAST_SELECTED_ENCODINGS_DEFAULT_MAX_SIZE = 5;
56+
private static final EncodingOptions INSTANCE = new EncodingOptions();
57+
private static final String ENCODING = "encoding"; // NOI18N
58+
private static final String LAST_SELECTED_ENCODINGS = "last.selected.encodings"; // NOI18N
59+
private static final String LAST_SELECTED_ENCODINGS_MAX_SIZE = "last.selected.encodings.max.size"; // NOI18N
60+
private static final String DELIMITER = "|"; // NOI18N
61+
62+
public static EncodingOptions getInstance() {
63+
return INSTANCE;
64+
}
65+
66+
private EncodingOptions() {
67+
}
68+
69+
public int getLastSelectedEncodingsMaxSize() {
70+
return getPreferences().getInt(LAST_SELECTED_ENCODINGS_MAX_SIZE, LAST_SELECTED_ENCODINGS_DEFAULT_MAX_SIZE);
71+
}
72+
73+
public void setLastSelectedEncodingsMazSize(int max) {
74+
getPreferences().putInt(LAST_SELECTED_ENCODINGS_MAX_SIZE, max);
75+
}
76+
77+
public List<String> getLastSelectedEncodings() {
78+
String encodingString = getPreferences().get(LAST_SELECTED_ENCODINGS, ""); // NOI18N
79+
if (encodingString.isEmpty()) {
80+
return Collections.emptyList();
81+
}
82+
String[] encodings = encodingString.split("\\" + DELIMITER); // NOI18N
83+
return Arrays.asList(encodings);
84+
}
85+
86+
public void setLastSelectedEncodings(String encoding) {
87+
List<String> selectedEncodings = new ArrayList<>(getLastSelectedEncodings());
88+
if (selectedEncodings.contains(encoding)) {
89+
selectedEncodings.remove(encoding);
90+
selectedEncodings.add(0, encoding);
91+
} else {
92+
if (selectedEncodings.size() >= getLastSelectedEncodingsMaxSize()) {
93+
selectedEncodings.remove(getLastSelectedEncodingsMaxSize() - 1);
94+
}
95+
selectedEncodings.add(0, encoding);
96+
}
97+
String join = String.join(DELIMITER, selectedEncodings);
98+
getPreferences().put(LAST_SELECTED_ENCODINGS, join);
99+
}
100+
101+
private Preferences getPreferences() {
102+
return NbPreferences.forModule(EncodingOptions.class).node(ENCODING);
103+
}
104+
}

src/com/junichi11/netbeans/modules/encoding/ui/EncodingPanel.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
*/
4040
package com.junichi11.netbeans.modules.encoding.ui;
4141

42+
import com.junichi11.netbeans.modules.encoding.options.EncodingOptions;
4243
import java.awt.Dimension;
4344
import java.awt.EventQueue;
4445
import java.awt.event.KeyAdapter;
@@ -47,6 +48,7 @@
4748
import java.awt.event.MouseListener;
4849
import java.nio.charset.Charset;
4950
import java.util.Collection;
51+
import java.util.List;
5052
import javax.swing.DefaultListModel;
5153
import javax.swing.JPanel;
5254
import javax.swing.event.DocumentEvent;
@@ -73,8 +75,23 @@ final class EncodingPanel extends JPanel {
7375
*/
7476
public EncodingPanel(Charset encoding) {
7577
initComponents();
78+
List<String> selectedEncodings = EncodingOptions.getInstance().getLastSelectedEncodings();
79+
// add last 5 selected encodings to top of the list by default
80+
selectedEncodings.forEach(e -> {
81+
if (!e.equals(encoding.name())) {
82+
encodingListModel.addElement(e);
83+
}
84+
});
85+
86+
// add the current encoding to top of the list
7687
CHARSETS.forEach((charset) -> {
77-
encodingListModel.addElement(charset.name());
88+
if (charset.name().equals(encoding.name())) {
89+
encodingListModel.add(0, charset.name());
90+
} else {
91+
if (!selectedEncodings.contains(charset.name())) {
92+
encodingListModel.addElement(charset.name());
93+
}
94+
}
7895
});
7996
encodingList.setModel(encodingListModel);
8097
encodingList.setSelectedValue(encoding.name(), true);
@@ -214,7 +231,6 @@ private void initComponents() {
214231
);
215232
}// </editor-fold>//GEN-END:initComponents
216233

217-
218234
// Variables declaration - do not modify//GEN-BEGIN:variables
219235
private javax.swing.JTextField encodingFilterTextField;
220236
private javax.swing.JList<String> encodingList;

src/com/junichi11/netbeans/modules/encoding/ui/EncodingStatusLineElementProvider.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
package com.junichi11.netbeans.modules.encoding.ui;
4141

4242
import com.junichi11.netbeans.modules.encoding.OpenInEncodingQueryImpl;
43+
import com.junichi11.netbeans.modules.encoding.options.EncodingOptions;
4344
import java.awt.AWTEvent;
4445
import java.awt.BorderLayout;
4546
import java.awt.Component;
@@ -171,6 +172,8 @@ private static void changeEncoding(String selectedEncoding) {
171172
fileObject.setAttribute(OpenInEncodingQueryImpl.ENCODING, selectedEncoding);
172173
final DataObject dobj = DataObject.find(fileObject);
173174

175+
// save selected encoding to options
176+
EncodingOptions.getInstance().setLastSelectedEncodings(selectedEncoding);
174177
UiUtils.reopen(dobj);
175178

176179
} catch (IOException ex) {

0 commit comments

Comments
 (0)