|
| 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 | +} |
0 commit comments