From a60744d913df198c7d517e05e56328826dd8537d Mon Sep 17 00:00:00 2001 From: junichi11 Date: Fri, 23 Mar 2018 09:31:34 +0900 Subject: [PATCH] Move EncodingAccessories below #14 --- nbproject/genfiles.properties | 4 +- nbproject/project.xml | 9 ++ .../encoding/actions/Bundle.properties | 2 +- .../encoding/actions/Bundle_es.properties | 2 +- .../encoding/actions/EncodingAccessories.form | 20 +--- .../encoding/actions/EncodingAccessories.java | 39 ++++--- .../encoding/actions/OpenInEncoding.java | 12 ++- .../encoding/actions/SaveInEncoding.java | 22 +++- .../encoding/ui/EncodingFileChooser.java | 82 ++++++++++++++ .../ui/EncodingMetalFileChooserUI.java | 100 ++++++++++++++++++ 10 files changed, 243 insertions(+), 49 deletions(-) create mode 100644 src/com/junichi11/netbeans/modules/encoding/ui/EncodingFileChooser.java create mode 100644 src/com/junichi11/netbeans/modules/encoding/ui/EncodingMetalFileChooserUI.java diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties index 1e599a3..2dd675b 100644 --- a/nbproject/genfiles.properties +++ b/nbproject/genfiles.properties @@ -1,8 +1,8 @@ -build.xml.data.CRC32=dc57ab9a +build.xml.data.CRC32=e12ca191 build.xml.script.CRC32=6c7bb501 build.xml.stylesheet.CRC32=a56c6a5b@2.72.1 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=dc57ab9a +nbproject/build-impl.xml.data.CRC32=e12ca191 nbproject/build-impl.xml.script.CRC32=3d1bfa64 nbproject/build-impl.xml.stylesheet.CRC32=238281d1@2.72.1 diff --git a/nbproject/project.xml b/nbproject/project.xml index 95108cc..8de7a05 100644 --- a/nbproject/project.xml +++ b/nbproject/project.xml @@ -49,6 +49,15 @@ 1.81.1.43 + + org.netbeans.modules.projectapi + + + + 1 + 1.66.1 + + org.netbeans.modules.queries diff --git a/src/com/junichi11/netbeans/modules/encoding/actions/Bundle.properties b/src/com/junichi11/netbeans/modules/encoding/actions/Bundle.properties index 9e2efd0..95a6ac1 100644 --- a/src/com/junichi11/netbeans/modules/encoding/actions/Bundle.properties +++ b/src/com/junichi11/netbeans/modules/encoding/actions/Bundle.properties @@ -1,6 +1,6 @@ CTL_OpenInEncoding=Open in Encoding ... CTL_SaveInEncoding=Save In Encoding... -EncodingAccessories.jLabel1.text=&Encoding +EncodingAccessories.jLabel1.text=&Encoding: TXT_OpenFile=Open File CTL_Open=Open MNE_Open=O diff --git a/src/com/junichi11/netbeans/modules/encoding/actions/Bundle_es.properties b/src/com/junichi11/netbeans/modules/encoding/actions/Bundle_es.properties index 5877306..9bd36cf 100644 --- a/src/com/junichi11/netbeans/modules/encoding/actions/Bundle_es.properties +++ b/src/com/junichi11/netbeans/modules/encoding/actions/Bundle_es.properties @@ -1,6 +1,6 @@ CTL_OpenInEncoding=Abrir en la codificaci\u00f3n ... CTL_SaveInEncoding=Guardar en la codificaci\u00f3n... -EncodingAccessories.jLabel1.text=&Codificaci\u00f3n +EncodingAccessories.jLabel1.text=&Codificaci\u00f3n: TXT_OpenFile=Abrir Archivo CTL_Open=Abrir MNE_Open=A diff --git a/src/com/junichi11/netbeans/modules/encoding/actions/EncodingAccessories.form b/src/com/junichi11/netbeans/modules/encoding/actions/EncodingAccessories.form index 259e6c0..686dd95 100644 --- a/src/com/junichi11/netbeans/modules/encoding/actions/EncodingAccessories.form +++ b/src/com/junichi11/netbeans/modules/encoding/actions/EncodingAccessories.form @@ -11,26 +11,10 @@ + - - - - - - - - - - - - - - - - - - + diff --git a/src/com/junichi11/netbeans/modules/encoding/actions/EncodingAccessories.java b/src/com/junichi11/netbeans/modules/encoding/actions/EncodingAccessories.java index a3a7a02..0a896d5 100644 --- a/src/com/junichi11/netbeans/modules/encoding/actions/EncodingAccessories.java +++ b/src/com/junichi11/netbeans/modules/encoding/actions/EncodingAccessories.java @@ -5,7 +5,9 @@ */ package com.junichi11.netbeans.modules.encoding.actions; +import com.junichi11.netbeans.modules.encoding.options.EncodingOptions; import java.awt.Component; +import java.awt.Dimension; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; @@ -34,6 +36,11 @@ public EncodingAccessories() { this.encoding.setRenderer(new EncodingRenderer()); } + public EncodingAccessories(Dimension dimension) { + this(); + jLabel1.setPreferredSize(dimension); + } + /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always @@ -45,24 +52,13 @@ private void initComponents() { jLabel1 = new javax.swing.JLabel(); encoding = new javax.swing.JComboBox(); + setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS)); + jLabel1.setLabelFor(encoding); org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(EncodingAccessories.class, "EncodingAccessories.jLabel1.text")); // NOI18N + add(jLabel1); - javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); - this.setLayout(layout); - layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(encoding, 0, 400, Short.MAX_VALUE) - .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE) - ); - layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addComponent(jLabel1) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(encoding, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) - ); + add(encoding); }// //GEN-END:initComponents public Charset getEncoding() { @@ -83,8 +79,17 @@ public EncodingModel() { final Collection acs = Charset.availableCharsets().values(); final List _data = new ArrayList<>(acs.size() + 1); _data.add(EncodingKey.DEFAULT); - acs.forEach((c) -> { - _data.add(new EncodingKey(c)); + List lastSelectedEncodings = EncodingOptions.getInstance().getLastSelectedEncodings(); + lastSelectedEncodings.forEach(encoding -> { + if (Charset.isSupported(encoding)) { + EncodingKey encodingKey = new EncodingKey(Charset.forName(encoding)); + _data.add(encodingKey); + } + }); + acs.forEach(charset -> { + if (!lastSelectedEncodings.contains(charset.name())) { + _data.add(new EncodingKey(charset)); + } }); data = Collections.unmodifiableList(_data); } diff --git a/src/com/junichi11/netbeans/modules/encoding/actions/OpenInEncoding.java b/src/com/junichi11/netbeans/modules/encoding/actions/OpenInEncoding.java index 3758454..7ada1a9 100644 --- a/src/com/junichi11/netbeans/modules/encoding/actions/OpenInEncoding.java +++ b/src/com/junichi11/netbeans/modules/encoding/actions/OpenInEncoding.java @@ -1,6 +1,8 @@ package com.junichi11.netbeans.modules.encoding.actions; +import com.junichi11.netbeans.modules.encoding.ui.EncodingFileChooser; import com.junichi11.netbeans.modules.encoding.OpenInEncodingQueryImpl; +import com.junichi11.netbeans.modules.encoding.options.EncodingOptions; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; @@ -30,17 +32,17 @@ public final class OpenInEncoding extends CallableSystemAction { @Override public void performAction() { - final JFileChooser chooser = new JFileChooser(); + final EncodingFileChooser chooser = new EncodingFileChooser(lastEncoding); chooser.setCurrentDirectory(null); chooser.setDialogTitle(NbBundle.getMessage(OpenInEncoding.class, "TXT_OpenFile")); chooser.setApproveButtonText(NbBundle.getMessage(OpenInEncoding.class, "CTL_Open")); chooser.setApproveButtonMnemonic(NbBundle.getMessage(OpenInEncoding.class, "MNE_Open").charAt(0)); chooser.setCurrentDirectory(lastFolder); - final EncodingAccessories acc = new EncodingAccessories(); - acc.setEncoding(lastEncoding); - chooser.setAccessory(acc); if (chooser.showOpenDialog(WindowManager.getDefault().getMainWindow()) == JFileChooser.APPROVE_OPTION) { - final Charset charset = acc.getEncoding(); + final Charset charset = chooser.getEncoding(); + if (charset != null) { + EncodingOptions.getInstance().setLastSelectedEncodings(charset.name()); + } lastEncoding = charset; lastFolder = chooser.getCurrentDirectory(); final File file = FileUtil.normalizeFile(chooser.getSelectedFile()); diff --git a/src/com/junichi11/netbeans/modules/encoding/actions/SaveInEncoding.java b/src/com/junichi11/netbeans/modules/encoding/actions/SaveInEncoding.java index 8fcb115..e682898 100644 --- a/src/com/junichi11/netbeans/modules/encoding/actions/SaveInEncoding.java +++ b/src/com/junichi11/netbeans/modules/encoding/actions/SaveInEncoding.java @@ -1,5 +1,6 @@ package com.junichi11.netbeans.modules.encoding.actions; +import com.junichi11.netbeans.modules.encoding.ui.EncodingFileChooser; import com.junichi11.netbeans.modules.encoding.OpenInEncodingQueryImpl; import java.io.File; import java.io.IOException; @@ -9,7 +10,11 @@ import java.io.Writer; import java.nio.charset.Charset; import javax.swing.JFileChooser; +import org.netbeans.api.project.FileOwnerQuery; +import org.netbeans.api.project.Project; import org.netbeans.api.queries.FileEncodingQuery; +import org.openide.DialogDisplayer; +import org.openide.NotifyDescriptor; import org.openide.awt.ActionID; import org.openide.awt.ActionReference; import org.openide.awt.ActionRegistration; @@ -37,22 +42,29 @@ public final class SaveInEncoding extends CookieAction { @Override protected void performAction(Node[] activatedNodes) { final DataObject dataObject = activatedNodes[0].getLookup().lookup(DataObject.class); + if (dataObject == null) { + return; + } FileObject fo = dataObject.getPrimaryFile(); + Project project = FileOwnerQuery.getOwner(fo); + // prevent freezing + if (project == null && !dataObject.isModified()) { + NotifyDescriptor.Message message = new NotifyDescriptor.Message("The file is not modified.", NotifyDescriptor.INFORMATION_MESSAGE); + DialogDisplayer.getDefault().notify(message); + return; + } File f = FileUtil.toFile(fo); if (f == null) { f = FileUtil.normalizeFile(new File(new File(System.getProperty("user.name")), fo.getNameExt())); } - final JFileChooser chooser = new JFileChooser(); + final EncodingFileChooser chooser = new EncodingFileChooser(); // Always suggest the default encoding chooser.setCurrentDirectory(null); chooser.setDialogTitle(NbBundle.getMessage(OpenInEncoding.class, "TXT_SaveFile")); chooser.setApproveButtonText(NbBundle.getMessage(OpenInEncoding.class, "CTL_Save")); chooser.setApproveButtonMnemonic(NbBundle.getMessage(OpenInEncoding.class, "MNE_Save").charAt(0)); chooser.setSelectedFile(f); - final EncodingAccessories acc = new EncodingAccessories(); - acc.setEncoding(null); //Always suggest the default encoding - chooser.setAccessory(acc); if (chooser.showSaveDialog(WindowManager.getDefault().getMainWindow()) == JFileChooser.APPROVE_OPTION) { - final Charset charset = acc.getEncoding(); + final Charset charset = chooser.getEncoding(); final String encodingName = (charset == null ? null : charset.name()); OpenInEncoding.lastFolder = chooser.getCurrentDirectory(); final File file = FileUtil.normalizeFile(chooser.getSelectedFile()); diff --git a/src/com/junichi11/netbeans/modules/encoding/ui/EncodingFileChooser.java b/src/com/junichi11/netbeans/modules/encoding/ui/EncodingFileChooser.java new file mode 100644 index 0000000..a306fdb --- /dev/null +++ b/src/com/junichi11/netbeans/modules/encoding/ui/EncodingFileChooser.java @@ -0,0 +1,82 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + */ +package com.junichi11.netbeans.modules.encoding.ui; + +import java.nio.charset.Charset; +import javax.swing.JFileChooser; + +/** + * + * @author junichi11 + */ +public class EncodingFileChooser extends JFileChooser { + + private static final long serialVersionUID = -6494800823794492445L; + + private EncodingMetalFileChooserUI fileChooserUI; + + public EncodingFileChooser() { + this(null); + } + + public EncodingFileChooser(Charset defaultEncoding) { + if (fileChooserUI != null) { + fileChooserUI.setDefaultEncoding(defaultEncoding); + } + } + + @Override + public void updateUI() { + super.updateUI(); + if (fileChooserUI == null) { + fileChooserUI = new EncodingMetalFileChooserUI(this); + } + setUI(fileChooserUI); + resetChoosableFileFilters(); + } + + public Charset getEncoding() { + if (fileChooserUI == null) { + return null; + } + return fileChooserUI.getEncoding(); + } + +} diff --git a/src/com/junichi11/netbeans/modules/encoding/ui/EncodingMetalFileChooserUI.java b/src/com/junichi11/netbeans/modules/encoding/ui/EncodingMetalFileChooserUI.java new file mode 100644 index 0000000..d9d9918 --- /dev/null +++ b/src/com/junichi11/netbeans/modules/encoding/ui/EncodingMetalFileChooserUI.java @@ -0,0 +1,100 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + */ +package com.junichi11.netbeans.modules.encoding.ui; + +import com.junichi11.netbeans.modules.encoding.actions.EncodingAccessories; +import java.awt.Component; +import java.awt.Container; +import java.awt.Dimension; +import java.nio.charset.Charset; +import javax.swing.Box; +import javax.swing.JFileChooser; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.plaf.metal.MetalFileChooserUI; + +/** + * + * @author junichi11 + */ +public class EncodingMetalFileChooserUI extends MetalFileChooserUI { + + private EncodingAccessories acc = new EncodingAccessories(); + + public EncodingMetalFileChooserUI(JFileChooser filechooser) { + super(filechooser); + } + + @Override + public void installComponents(JFileChooser fileChooser) { + super.installComponents(fileChooser); + JPanel bottomPanel = getBottomPanel(); + Dimension preferredSize = getPreferredJLabelSize(bottomPanel); + if (preferredSize != null) { + acc = new EncodingAccessories(preferredSize); + } + bottomPanel.add(Box.createRigidArea(new Dimension(1, 5)), 3); + bottomPanel.add(acc, 4); + } + + void setDefaultEncoding(Charset defaultEncoding) { + acc.setEncoding(defaultEncoding); + } + + public Charset getEncoding() { + return acc.getEncoding(); + } + + private Dimension getPreferredJLabelSize(Container container) { + Component[] components = container.getComponents(); + for (Component component : components) { + if (component instanceof JLabel) { + return component.getPreferredSize(); + } + if (component instanceof Container) { + Dimension preferredDimension = getPreferredJLabelSize((Container) component); + if (preferredDimension != null) { + return preferredDimension; + } + } + } + return null; + } +}