Skip to content

Commit

Permalink
Version 1.1.2: Complete localization
Browse files Browse the repository at this point in the history
- HBiede
  • Loading branch information
hbiede committed Oct 10, 2019
1 parent 3c818b1 commit f580cdf
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 42 deletions.
24 changes: 15 additions & 9 deletions src/com/hbiede/ContactPhotos.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.swing.*;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class ContactPhotos extends SwingWorker<Void, Void> {
Expand All @@ -23,6 +24,9 @@ public class ContactPhotos extends SwingWorker<Void, Void> {
@NonNls
private static final String IO_PROBLEM_RESOURCE_KEY = "IOProblem";
private static final char SPACE_CHAR = ' ';
@NonNls
public static final String RUN_BUTTON_TEXT = "RunButtonText";
private static final char VCF_FIELD_DELIMITER = ':';
private static ContactGUI gui = new ContactGUI();
private File inputFile = null;
private File outputDirectory = null;
Expand All @@ -49,9 +53,9 @@ private static int countContacts(File contactsFile) {
BufferedReader br;
int contactsCount = 0;
try {
br = new BufferedReader(new FileReader(contactsFile));
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
br = new BufferedReader(new FileReader(contactsFile, StandardCharsets.UTF_8));
} catch (IOException ioe) {
ioe.printStackTrace();
return 0;
}

Expand Down Expand Up @@ -104,8 +108,8 @@ private void setTotalContacts(int totalContacts) {
protected Void doInBackground() {
BufferedReader br;
try {
br = new BufferedReader(new FileReader(inputFile));
} catch (FileNotFoundException e) {
br = new BufferedReader(new FileReader(inputFile, StandardCharsets.UTF_8));
} catch (IOException ioe) {
System.out.println(Localizer.i18n_str("RequestValidVCF"));
return null;
}
Expand Down Expand Up @@ -137,7 +141,7 @@ protected Void doInBackground() {
} else if (newLine.contains(PHOTO)) {
// Start a new photo string
photoString.delete(0, photoString.length());
photoString.append(newLine.substring(newLine.indexOf(':') + 1));
photoString.append(newLine.substring(newLine.indexOf(VCF_FIELD_DELIMITER) + 1));
photoInProgress = true;
} else if (photoInProgress && newLine.charAt(0) == SPACE_CHAR) {
// Add to the existing photo string if the line starts with a space (an indent
Expand Down Expand Up @@ -167,7 +171,8 @@ protected Void doInBackground() {
os.close();
contactName = "";
} catch (Exception e) {
System.out.printf(Localizer.i18n_str("BrokenPhoto") + "\n", contactName);
System.out.printf(Localizer.i18n_str("BrokenPhoto"), contactName);
System.out.println(); // new line character
System.err.println(e.getMessage());
continue;
}
Expand All @@ -185,8 +190,9 @@ protected Void doInBackground() {

@Override
protected void done() {
gui.runButton.setActionCommand("run");
gui.runButton.setText("Run");
@NonNls String terminatingActionCommand = "run";
gui.runButton.setActionCommand(terminatingActionCommand);
gui.runButton.setText(Localizer.i18n_str(RUN_BUTTON_TEXT));
gui.fileButton.setEnabled(true);
gui.directoryButton.setEnabled(true);
}
Expand Down
4 changes: 2 additions & 2 deletions src/com/hbiede/Localizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import java.text.MessageFormat;
import java.util.ResourceBundle;

final class Localizer {
public final class Localizer {
@NonNls
private static final String LOCALES_DIR = "locales.contactsOutput";
@NonNls
private static final ResourceBundle resourseBundle = ResourceBundle.getBundle(LOCALES_DIR);

static String i18n_str(@PropertyKey(resourceBundle = LOCALES_DIR) String key, Object... params) {
public static String i18n_str(@PropertyKey(resourceBundle = LOCALES_DIR) String key, Object... params) {
String value = resourseBundle.getString(key);

if (params.length > 0) return MessageFormat.format(value, params);
Expand Down
24 changes: 12 additions & 12 deletions src/com/hbiede/gui/ContactGUI.form
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.hbiede.gui.ContactGUI">
<grid id="27dc6" binding="mainPanel" layout-manager="GridBagLayout">
<constraints>
<xy x="20" y="20" width="508" height="400"/>
<xy x="20" y="20" width="786" height="400"/>
</constraints>
<properties/>
<border type="none"/>
Expand All @@ -17,8 +17,8 @@
<maximumSize width="120" height="30"/>
<minimumSize width="120" height="30"/>
<preferredSize width="120" height="30"/>
<text value="VCF File"/>
<toolTipText value="Choose a VCF File to process"/>
<text resource-bundle="locales/contactsOutput" key="FileButtonText"/>
<toolTipText resource-bundle="locales/contactsOutput" key="FileButtonTooltip"/>
</properties>
</component>
<hspacer id="3f869">
Expand All @@ -40,8 +40,8 @@
</constraints>
<properties>
<actionCommand value="dir"/>
<text value="Output Folder"/>
<toolTipText value="Choose a folder to which to output the photos"/>
<text resource-bundle="locales/contactsOutput" key="DirectoryButtonText"/>
<toolTipText resource-bundle="locales/contactsOutput" key="DirectoryButtonTooltip"/>
</properties>
</component>
<hspacer id="4e737">
Expand All @@ -65,8 +65,8 @@
</constraints>
<properties>
<actionCommand value="FirstLast"/>
<text value="First Last"/>
<toolTipText value="ie John Smith"/>
<text resource-bundle="locales/contactsOutput" key="FirstLastOptionText"/>
<toolTipText resource-bundle="locales/contactsOutput" key="FirstLastOptionTooltip"/>
</properties>
</component>
<component id="d4199" class="javax.swing.JRadioButton" binding="lastFirstRadioButton" default-binding="true">
Expand All @@ -77,8 +77,8 @@
<properties>
<actionCommand value="LastFirst"/>
<selected value="true"/>
<text value="Last, First"/>
<toolTipText value="ie Smith, John"/>
<text resource-bundle="locales/contactsOutput" key="LastFirstOptionText"/>
<toolTipText resource-bundle="locales/contactsOutput" key="LastFirstOption"/>
</properties>
</component>
<component id="a5da" class="javax.swing.JLabel" binding="radioTitleLabel">
Expand All @@ -87,7 +87,7 @@
<gridbag weightx="0.0" weighty="0.0"/>
</constraints>
<properties>
<text value="Output Name:"/>
<text resource-bundle="locales/contactsOutput" key="RadioOptionText"/>
</properties>
</component>
</children>
Expand All @@ -103,7 +103,7 @@
<maximumSize width="90" height="30"/>
<minimumSize width="90" height="30"/>
<preferredSize width="83" height="30"/>
<text value="Run"/>
<text resource-bundle="locales/contactsOutput" key="RunButtonText"/>
</properties>
</component>
<component id="8c9cc" class="javax.swing.JProgressBar" binding="progressBar">
Expand All @@ -126,7 +126,7 @@
<enabled value="true"/>
<horizontalAlignment value="0"/>
<horizontalTextPosition value="0"/>
<text value="000 contact photos exported before cancelling"/>
<text resource-bundle="locales/contactsOutput" key="DefaultLabelText"/>
<visible value="true"/>
</properties>
</component>
Expand Down
46 changes: 31 additions & 15 deletions src/com/hbiede/gui/ContactGUI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.hbiede.gui;

import com.hbiede.ContactPhotos;
import com.hbiede.Localizer;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
Expand Down Expand Up @@ -34,6 +36,19 @@ public class ContactGUI {
private File inputFile;
private File outputDirectory;

@NonNls
private static final String PROGRESS_EVENT_MESSAGE = "progress";
@NonNls
private static final String CANCEL_ACTION_COMMAND = "cancel";
@NonNls
private static final String RUN_ACTION_COMMAND = "run";
@NonNls
private static final String V_CARD_DESCRIPTION = "vCard";
@NonNls
private static final String VCF_EXTENSION = "vcf";
@NonNls
private static final String LAST_FIRST_ACTION_CMD = "LastFirst";


public ContactGUI() {
super();
Expand All @@ -51,9 +66,10 @@ public void setFrame(JFrame frame) {
}

private class RadioListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
outputNameLastFirst = "LastFirst".equals(e.getActionCommand());
outputNameLastFirst = LAST_FIRST_ACTION_CMD.equals(e.getActionCommand());
outputTask.setOutputNameLastFirst(outputNameLastFirst);
}
}
Expand All @@ -69,7 +85,7 @@ public void actionPerformed(ActionEvent e) {
// Open File selection window
final JFileChooser fc = new JFileChooser();
// Only allow vCards
fc.setFileFilter(new FileNameExtensionFilter("vCard", "vcf"));
fc.setFileFilter(new FileNameExtensionFilter(V_CARD_DESCRIPTION, VCF_EXTENSION));
// Only set the file location if it is an affirmative selection
int returnVal = fc.showOpenDialog(mainPanel);
if (returnVal == JFileChooser.APPROVE_OPTION) {
Expand All @@ -78,15 +94,15 @@ public void actionPerformed(ActionEvent e) {
// Check if there is enough free space on the disk to store the photos
// Working under the assumption that inputFile's file size serves as a
if (inputFile.length() >= inputFile.getFreeSpace()) {
JOptionPane.showMessageDialog(new JFrame("Not Enough Space"), "Not Enough Free Space!\nPlease clear out some hard drive space.", "Dialog",
JOptionPane.showMessageDialog(new JFrame(Localizer.i18n_str("InsufficientSpaceError")), Localizer.i18n_str("InsufficientSpaceErrorExplanation"), Localizer.i18n_str("InsufficientSpaceWindowTitle"),
JOptionPane.ERROR_MESSAGE);
inputFile = null;
} else {
// Give a warning if the file is rather large (100MB+)
if (inputFile.length() > LARGE_SIZE_FILE_LENGTH) {
contactCountLabel.setText("Loading...");
contactCountLabel.setText(Localizer.i18n_str("LoadingMessage"));
contactCountLabel.setVisible(true);
System.out.println("Big file");
System.out.println(Localizer.i18n_str("BigFileWarning"));
}

outputTask.setInputFile(inputFile);
Expand All @@ -103,23 +119,23 @@ public void actionPerformed(ActionEvent e) {
outputDirectory = fc.getSelectedFile();
outputTask.setOutputDirectory(outputDirectory);
}
} else if ("run".equals(e.getActionCommand())) {
} else if (RUN_ACTION_COMMAND.equals(e.getActionCommand())) {
// Run Photo Output
progressBar.setValue(0);
contactCountLabel.setText("0 contact photos exported");
contactCountLabel.setText(Localizer.i18n_str("ZeroExportText"));

// allow the action to be stopable
runButton.setActionCommand("cancel");
runButton.setText("Cancel");
runButton.setActionCommand(CANCEL_ACTION_COMMAND);
runButton.setText(Localizer.i18n_str("CancelButtonText"));

// setup the threaded task
outputTask.addPropertyChangeListener(this);
if (outputTask.isRunnable()) outputTask.execute();
else {
//restore properties to non-running state and disable run button
contactCountLabel.setText("Problem Occurred with Property Setting");
runButton.setActionCommand("run");
runButton.setText("Run");
contactCountLabel.setText(Localizer.i18n_str("PropertySettingErrorMessage"));
runButton.setActionCommand(RUN_ACTION_COMMAND);
runButton.setText(Localizer.i18n_str(ContactPhotos.RUN_BUTTON_TEXT));
runButton.setEnabled(false);
}

Expand All @@ -131,7 +147,7 @@ public void actionPerformed(ActionEvent e) {
// cancel button pressed
outputTask.cancel(true);
contactCountLabel.setVisible(true);
contactCountLabel.setText(String.format("%d contact photos exported before cancelling", outputTask.getContactsOutputSoFar()));
contactCountLabel.setText(String.format(Localizer.i18n_str("ExportCountBeforeCancel"), outputTask.getContactsOutputSoFar()));
frame.pack();

//reenable buttons
Expand All @@ -151,10 +167,10 @@ public void actionPerformed(ActionEvent e) {

@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equals(evt.getPropertyName())) {
if (PROGRESS_EVENT_MESSAGE.equals(evt.getPropertyName())) {
int progress = (Integer) evt.getNewValue();
progressBar.setValue(progress);
contactCountLabel.setText(String.format("%d contact photos exported", outputTask.getContactsOutputSoFar()));
contactCountLabel.setText(String.format(Localizer.i18n_str("ExportCount"), outputTask.getContactsOutputSoFar()));
}
}
}
Expand Down
25 changes: 23 additions & 2 deletions src/locales/contactsOutput.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
BrokenPhoto=Broken photo on contact \\"%s\\"\n
BigFileWarning=Big file
BrokenPhoto=Broken photo on contact "%s"
CancelButtonText=Cancel
DefaultLabelText=000 contact photos exported before cancelling
DirectoryButtonText=Output Flolder
DirectoryButtonTooltip=Choose a folder to which to output the photos
ExportCount=%d contact photos exported
ExportCountBeforeCancel=%d contact photos exported before cancelling
FileButtonText=VCF File
FileButtonTooltip=Choose a VCF File to process
FirstLastOptionText=First Last
FirstLastOptionTooltip=ie John Smith
FrameTitle=Contact Photos
InsufficientSpaceWindowTitle=Insufficient Space
InsufficientSpaceError=Not Enough Space
InsufficientSpaceErrorExplanation=Not Enough Free Space!\nPlease clear out some hard drive space.
IOProblem=Well, that's a problem...
RequestValidVCF=Please input a valid vCard file.
LastFirstOption=ie Smith, John
LastFirstOptionText=Last, First
LoadingMessage=Loading...
PropertySettingErrorMessage=Problem Occurred with Setting Properties
RadioOptionText=Output Name:
RequestValidVCF=Please input a valid vCard file.
RunButtonText=Run
ZeroExportText=0 contact photos exported
25 changes: 23 additions & 2 deletions src/locales/contactsOutput_es.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
BrokenPhoto=Photo malformado por \\"%s\\"
BigFileWarning=Archivo tan grande
BrokenPhoto=Photo malformado por "%s"
CancelButtonText=Cancel
DefaultLabelText=000 fotos de contactos exportado antes de cancelando
DirectoryButtonText=Carpeta Producción
DirectoryButtonTooltip=Escoge una carpeta a donde poner los fotos
ExportCount=%d fotos de contactos exportado antes de cancelando
ExportCountBeforeCancel=%d fotos de contactos exportado antes de cancelando
FileButtonText=Archivo de VCF
FileButtonTooltip=Escoge un archivo VCF a procesar
FirstLastOptionText=Nombre Apellido
FirstLastOptionTooltip=como John Smith
FrameTitle=Contact Photos
InsufficientSpaceWindowTitle=Espacio Insuficiente
InsufficientSpaceError=Espacio Insuficiente
InsufficientSpaceErrorExplanation=Espacio insuficiente en disco!\nHacer escpacio.
IOProblem=Tenemos un problemo...
RequestValidVCF=Por favor, ingresa un VCF
LastFirstOption=como Smith, John
LastFirstOptionText=Apellido, Nombre
LoadingMessage=Cargando...
PropertySettingErrorMessage=Había un Problemo con Ajustiando Ajustes
RadioOptionText=Nombre Mostrado:
RequestValidVCF=Por favor, ingresa un VCF
RunButtonText=Va
ZeroExportText=0 fotos de contactos exportado

0 comments on commit f580cdf

Please sign in to comment.