Skip to content

Commit

Permalink
Move EncodingAccessories below #14
Browse files Browse the repository at this point in the history
  • Loading branch information
junichi11 committed Mar 23, 2018
1 parent 0a69e7a commit a60744d
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 49 deletions.
4 changes: 2 additions & 2 deletions nbproject/genfiles.properties
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@
<specification-version>1.81.1.43</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.projectapi</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>1</release-version>
<specification-version>1.66.1</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.queries</code-name-base>
<build-prerequisite/>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,10 @@
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
<AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,0,34,0,0,1,55"/>
</AuxValues>

<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="encoding" alignment="0" pref="400" max="32767" attributes="0"/>
<Component id="jLabel1" alignment="0" pref="400" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Component id="jLabel1" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="encoding" min="-2" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<Layout class="org.netbeans.modules.form.compat2.layouts.DesignBoxLayout"/>
<SubComponents>
<Component class="javax.swing.JLabel" name="jLabel1">
<Properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
}// </editor-fold>//GEN-END:initComponents

public Charset getEncoding() {
Expand All @@ -83,8 +79,17 @@ public EncodingModel() {
final Collection<? extends Charset> acs = Charset.availableCharsets().values();
final List<EncodingKey> _data = new ArrayList<>(acs.size() + 1);
_data.add(EncodingKey.DEFAULT);
acs.forEach((c) -> {
_data.add(new EncodingKey(c));
List<String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}
Loading

0 comments on commit a60744d

Please sign in to comment.