import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
+import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
- // Components
+ // Components
private JFileChooser fileChooserSrcFile;
+ private JFileChooser fileChooserOutputFile;
private JLabel labelSrcFile;
private JTextField textFieldSrcFileName;
private JLabel labelSrcFileType;
private CanvasPreview canvasPreview;
private JLabel labelOutputFileName;
private JTextField textFieldOutputFileName;
- private JLabel labelMessage;
+ //private JLabel labelMessage;
private JButton buttonOutput;
// Variables
private float paperMargin;
private ArrayList<Double> listPaperMarginUnit = new ArrayList<Double>();
- private String generateFileName(String str) {
- str = str.replace("{pwd}", System.getProperty("user.dir"));
- if (this.srcFile != null) {
- str = str.replace("{input}", this.srcFile.getName());
- }
- str = str.replace("{date}", new SimpleDateFormat("yyyyMMddHHmmss")
- .format(new Date()));
- return str;
+ private String generateFileName(String str) {
+ str = str.replace("{pwd}", System.getProperty("user.dir"));
+ if (this.srcFile != null) {
+ str = str.replace("{input}", this.srcFile.getName());
}
-
+ str = str.replace("{date}", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
+ return str;
+ }
+
public File getSrcFile() {
return this.srcFile;
}
- public boolean checkFileLoaded() {
- return this.ic != null;
+ public boolean checkFileLoaded() {
+ return this.ic != null;
+ }
+
+ private void simulateOutput() {
+ // 設定値が変更されたときに canvasPreview 等を更新する
+
+ //labelMessage.setText("");
+
+ PdfGenerator.CreatePosterArgs args = new PdfGenerator.CreatePosterArgs();
+ args.setSrcRect(srcLeft, srcBottom, srcRight, srcTop);
+ args.setPosterSize(posterWidth, posterHeight);
+ args.setPaperSize(paperWidth, paperHeight);
+ args.setMargin(paperMargin, paperMargin, paperMargin, paperMargin);
+
+ int outputPaperCount = PdfGenerator.simulateOutputPaperCount(args);
+ numberFieldOutputPaperCount.setValue(outputPaperCount);
+
+ canvasPreview.setArgs(args);
+ canvasPreview.repaint();
+ }
+
+ private void optimizePaperRotated() {
+ // 出力枚数が少なくなるように印刷用紙の向きを更新する
+
+ PdfGenerator.CreatePosterArgs args1 = new PdfGenerator.CreatePosterArgs();
+ args1.setSrcRect(srcLeft, srcBottom, srcRight, srcTop);
+ args1.setPosterSize(posterWidth, posterHeight);
+ args1.setPaperSize(paperWidth, paperHeight);
+ args1.setMargin(paperMargin, paperMargin, paperMargin, paperMargin);
+ int outputPaperCount1 = PdfGenerator.simulateOutputPaperCount(args1);
+ if (outputPaperCount1 < 0) {
+ // 不正なとき
+ outputPaperCount1 = Integer.MAX_VALUE;
}
-
- private void simulateOutput() {
- // 設定値が変更されたときに canvasPreview 等を更新する
-
- labelMessage.setText("");
-
- PdfGenerator.CreatePosterArgs args = new PdfGenerator.CreatePosterArgs();
- args.setSrcRect(srcLeft, srcBottom, srcRight, srcTop);
- args.setPosterSize(posterWidth, posterHeight);
- args.setPaperSize(paperWidth, paperHeight);
- args.setMargin(paperMargin, paperMargin, paperMargin, paperMargin);
-
- int outputPaperCount = PdfGenerator.simulateOutputPaperCount(args);
- numberFieldOutputPaperCount.setValue(outputPaperCount);
-
- canvasPreview.setArgs(args);
- canvasPreview.repaint();
+
+ PdfGenerator.CreatePosterArgs args2 = new PdfGenerator.CreatePosterArgs();
+ args2.setSrcRect(srcLeft, srcBottom, srcRight, srcTop);
+ args2.setPosterSize(posterWidth, posterHeight);
+ args2.setPaperSize(paperHeight, paperWidth); // 印刷用紙を回転する
+ args2.setMargin(paperMargin, paperMargin, paperMargin, paperMargin);
+ int outputPaperCount2 = PdfGenerator.simulateOutputPaperCount(args2);
+ if (outputPaperCount2 < 0) {
+ outputPaperCount2 = Integer.MAX_VALUE;
}
-
- private void optimizePaperRotated() {
- // 出力枚数が少なくなるように印刷用紙の向きを更新する
-
- PdfGenerator.CreatePosterArgs args1 = new PdfGenerator.CreatePosterArgs();
- args1.setSrcRect(srcLeft, srcBottom, srcRight, srcTop);
- args1.setPosterSize(posterWidth, posterHeight);
- args1.setPaperSize(paperWidth, paperHeight);
- args1.setMargin(paperMargin, paperMargin, paperMargin, paperMargin);
- int outputPaperCount1 = PdfGenerator.simulateOutputPaperCount(args1);
- if (outputPaperCount1 < 0) {
- // 不正なとき
- outputPaperCount1 = Integer.MAX_VALUE;
- }
-
- PdfGenerator.CreatePosterArgs args2 = new PdfGenerator.CreatePosterArgs();
- args2.setSrcRect(srcLeft, srcBottom, srcRight, srcTop);
- args2.setPosterSize(posterWidth, posterHeight);
- args2.setPaperSize(paperHeight, paperWidth); // 印刷用紙を回転する
- args2.setMargin(paperMargin, paperMargin, paperMargin, paperMargin);
- int outputPaperCount2 = PdfGenerator.simulateOutputPaperCount(args2);
- if (outputPaperCount2 < 0) {
- outputPaperCount2 = Integer.MAX_VALUE;
- }
-
- if (outputPaperCount1 > outputPaperCount2
- || (outputPaperCount1 == outputPaperCount2 && paperRotated)) {
- // 最適化
- checkBoxPaperRotate.setSelected(!paperRotated);
- checkBoxPaperRotate_itemStateChanged();
- }
+
+ if ((outputPaperCount1 > outputPaperCount2)
+ || (outputPaperCount1 == outputPaperCount2 && paperRotated)) {
+ // 最適化
+ checkBoxPaperRotate.setSelected(!paperRotated);
+ checkBoxPaperRotate_itemStateChanged();
}
-
- public void outputPdf() {
- if (!checkFileLoaded()) {
- labelMessage.setText(Lib.getMessage("Text.NoInputFiles"));
- this.validateTree();
- return;
- }
-
- PdfGenerator.CreatePosterArgs args = new PdfGenerator.CreatePosterArgs();
- args.setSrcRect(srcLeft, srcBottom, srcRight, srcTop);
- args.setPosterSize(posterWidth, posterHeight);
- args.setPaperSize(paperWidth, paperHeight);
- args.setMargin(paperMargin, paperMargin, paperMargin, paperMargin);
- String outputFileName = generateFileName(textFieldOutputFileName
- .getText());
- try {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- PdfGenerator.createPoster(ic, args, baos);
- try {
- FileOutputStream fos = new FileOutputStream(outputFileName);
- baos.writeTo(fos);
- fos.close();
-
- labelMessage.setText(String.format(
- Lib.getMessage("Text.Saved"), outputFileName));
- } catch (IOException ex) {
- labelMessage.setText(String.format(Lib
- .getMessage("Text.FailedWrite"), outputFileName));
- }
- } catch (Exception ex) {
- labelMessage.setText(Lib.getMessage("Text.FailedGenerate"));
- }
- this.validateTree();
+ }
+
+ public void outputPdf(File file) {
+ if (!checkFileLoaded()) {
+ return;
}
-
- private void changePageSuccess(ImageContainer ic) {
- this.ic = ic;
-
- comboBoxSrcPageNum.setSelectedIndex(ic.getPageNum() - 1);
-
- srcImageWidth = ic.getImageWidth();
- srcImageHeight = ic.getImageHeight();
- numberFieldSrcImageSize_setValue();
-
- comboBoxSrcImageSizeUnit.setEnabled(true);
- checkBoxSrcRectWholeOfPage.setEnabled(true);
- comboBoxSrcRectUnit.setEnabled(true);
- buttonPaperOptimize.setEnabled(true);
-
- canvasPreview.setImageContainer(ic);
- simulateOutput();
+
+ PdfGenerator.CreatePosterArgs args = new PdfGenerator.CreatePosterArgs();
+ args.setSrcRect(srcLeft, srcBottom, srcRight, srcTop);
+ args.setPosterSize(posterWidth, posterHeight);
+ args.setPaperSize(paperWidth, paperHeight);
+ args.setMargin(paperMargin, paperMargin, paperMargin, paperMargin);
+ try {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ PdfGenerator.createPoster(ic, args, baos);
+ try {
+ FileOutputStream fos = new FileOutputStream(file);
+ baos.writeTo(fos);
+ fos.close();
+
+ //labelMessage.setText(String.format(Lib.getMessage("Text.Saved"), outputFileName));
+ } catch (IOException ex) {
+ showErrorMessage(String.format(Lib.getMessage("Text.FailedWrite"), Lib.fileGetCanonicalPath(file)));
+ //labelMessage.setText(String.format(Lib.getMessage("Text.FailedWrite"), outputFileName));
+ }
+ } catch (Exception ex) {
+ showErrorMessage(Lib.getMessage("Text.FailedGenerate"));
+ //labelMessage.setText(Lib.getMessage("Text.FailedGenerate"));
}
+ //this.validateTree();
+ }
- public void changePage(int pageNum) {
- if (fc == null) {
- return;
- }
- try {
- ImageContainer ic = new ImageContainer(fc, pageNum);
- changePageSuccess(ic);
- } catch (Exception ex) {
-
- }
- this.validateTree();
+ private void changePageSuccess(ImageContainer ic) {
+ this.ic = ic;
+
+ comboBoxSrcPageNum.setSelectedIndex(ic.getPageNum() - 1);
+
+ srcImageWidth = ic.getImageWidth();
+ srcImageHeight = ic.getImageHeight();
+ numberFieldSrcImageSize_setValue();
+
+ comboBoxSrcImageSizeUnit.setEnabled(true);
+ checkBoxSrcRectWholeOfPage.setEnabled(true);
+ comboBoxSrcRectUnit.setEnabled(true);
+ buttonPaperOptimize.setEnabled(true);
+ buttonOutput.setEnabled(true);
+
+ canvasPreview.setImageContainer(ic);
+ simulateOutput();
+ }
+
+ public void changePage(int pageNum) {
+ if (fc == null) {
+ return;
}
-
- private void changeFileSuccess(File file, FileContainer fc) {
- this.srcFile = file;
- this.fc = fc;
-
- try {
- textFieldSrcFileName.setText(file.getCanonicalPath());
- } catch (Exception ex) {
- textFieldSrcFileName.setText(file.getAbsolutePath());
- }
-
- labelSrcFileType.setText(Lib.getFileTypeName(fc.getFileType()));
-
- comboBoxSrcPageNum_initialize(fc.getPageCount());
- comboBoxSrcPageNum.setSelectedIndex(0);
- comboBoxSrcPageNum.setEnabled(fc.getFileType() == Lib.FT_PDF);
-
- comboBoxSrcImageSizeUnit_initialize(fc.getFileType());
- comboBoxSrcImageSizeUnit.setSelectedIndex(0);
-
- comboBoxSrcRectUnit_initialize(fc.getFileType());
- comboBoxSrcRectUnit.setSelectedIndex(0);
-
- changePage(comboBoxSrcPageNum.getSelectedIndex() + 1);
-
- checkBoxSrcRectWholeOfPage.setSelected(true);
- checkBoxSrcRectWholeOfPage_itemStateChanged();
+ try {
+ ImageContainer ic = new ImageContainer(fc, pageNum);
+ changePageSuccess(ic);
+ } catch (Exception ex) {
+
}
-
+ //this.validateTree();
+ }
+
+ private void changeFileSuccess(File file, FileContainer fc) {
+ this.srcFile = file;
+ this.fc = fc;
+
+ textFieldSrcFileName.setText(Lib.fileGetCanonicalPath(file));
+
+ labelSrcFileType.setText(Lib.getFileTypeName(fc.getFileType()));
+
+ comboBoxSrcPageNum_initialize(fc.getPageCount());
+ comboBoxSrcPageNum.setSelectedIndex(0);
+ comboBoxSrcPageNum.setEnabled(fc.getFileType() == Lib.FT_PDF);
+
+ comboBoxSrcImageSizeUnit_initialize(fc.getFileType());
+ comboBoxSrcImageSizeUnit.setSelectedIndex(0);
+
+ comboBoxSrcRectUnit_initialize(fc.getFileType());
+ comboBoxSrcRectUnit.setSelectedIndex(0);
+
+ changePage(comboBoxSrcPageNum.getSelectedIndex() + 1);
+
+ checkBoxSrcRectWholeOfPage.setSelected(true);
+ checkBoxSrcRectWholeOfPage_itemStateChanged();
+ }
+
public void changeFile(File file) {
try {
FileContainer fc = new FileContainer(file);
changeFileSuccess(file, fc);
} catch (Exception ex) {
- String filePath;
- try {
- filePath = file.getCanonicalPath();
- } catch (Exception ex2) {
- filePath = file.getAbsolutePath();
- }
- labelMessage.setText(String.format(Lib.getMessage("Text.FailedOpen"), filePath));
+ showErrorMessage(String.format(Lib.getMessage("Text.FailedOpen"), Lib.fileGetCanonicalPath(file)));
+ //labelMessage.setText(String.format(Lib.getMessage("Text.FailedOpen"), Lib.fileGetCanonicalPath(file)));
}
- this.validateTree();
+ //this.validateTree();
}
-
- private void updatePosterSize() {
- // preferredPosterSize から posterSize を更新する
- // KeepAspectRatio なら srcRect を拡大して preferredPosterSize に入る最大の Size を
- // posterSize とする
- posterWidth = preferredPosterWidth;
- posterHeight = preferredPosterHeight;
- if (checkBoxPosterSizeKeepAspectRatio.isSelected()) {
- if ((srcRight - srcLeft) * preferredPosterHeight > (srcTop - srcBottom)
- * preferredPosterWidth) {
- // Src が横長
- if (srcRight != srcLeft) {
- posterHeight = posterWidth * (srcTop - srcBottom)
- / (srcRight - srcLeft);
- }
- } else {
- if (srcTop != srcBottom) {
- posterWidth = posterHeight * (srcRight - srcLeft)
- / (srcTop - srcBottom);
- }
- }
+
+ private void updatePosterSize() {
+ // preferredPosterSize から posterSize を更新する
+ // KeepAspectRatio なら srcRect を拡大して preferredPosterSize に入る最大の Size を
+ // posterSize とする
+ posterWidth = preferredPosterWidth;
+ posterHeight = preferredPosterHeight;
+ if (checkBoxPosterSizeKeepAspectRatio.isSelected()) {
+ if ((srcRight - srcLeft) * preferredPosterHeight > (srcTop - srcBottom) * preferredPosterWidth) {
+ // Src が横長
+ if (srcRight != srcLeft) {
+ posterHeight = posterWidth * (srcTop - srcBottom) / (srcRight - srcLeft);
+ }
+ } else {
+ if (srcTop != srcBottom) {
+ posterWidth = posterHeight * (srcRight - srcLeft) / (srcTop - srcBottom);
}
+ }
}
+ }
/*
* private void revisePosterSize(int direction) { // direction は固定する方向
* (changed) { numberFieldPosterSize_setValue(); simulateOutput(); } }
*/
- private File fileChooserSrcFile_show()
- {
+ private void showErrorMessage(String msg) {
+ JOptionPane.showMessageDialog(this, msg, Lib.getMessage("App.LowerTitle"), JOptionPane.ERROR_MESSAGE);
+ }
+
+ private File fileChooserSrcFile_show() {
if (fileChooserSrcFile.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
return fileChooserSrcFile.getSelectedFile();
} else {
this.dispose();
}
- private void menuItemOutput_actionPerformed() {
- outputPdf();
+ private void menuItemOutput_actionPerformed() {
+ if (!checkFileLoaded()) {
+ //labelMessage.setText(Lib.getMessage("Text.NoInputFiles"));
+ //this.validateTree();
+ return;
+ }
+
+ String outputFileName = generateFileName(textFieldOutputFileName.getText());
+ fileChooserOutputFile.setSelectedFile(new File(outputFileName));
+ if (fileChooserOutputFile.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
+ return;
}
+ outputPdf(fileChooserOutputFile.getSelectedFile());
+ }
private void menuItemExit_actionPerformed() {
System.exit(0);
optimizePaperRotated();
}
- private void buttonOutput_actionPerformed() {
- outputPdf();
+ private void buttonOutput_actionPerformed() {
+ if (!checkFileLoaded()) {
+ //labelMessage.setText(Lib.getMessage("Text.NoInputFiles"));
+ //this.validateTree();
+ return;
+ }
+
+ String outputFileName = generateFileName(textFieldOutputFileName.getText());
+ fileChooserOutputFile.setSelectedFile(new File(outputFileName));
+ if (fileChooserOutputFile.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
+ return;
}
+ outputPdf(fileChooserOutputFile.getSelectedFile());
+ }
private void numberField_initialize(NumberField numberField) {
numberField.setColumns(7);
}
});
- fileChooserSrcFile = new JFileChooser();
FileFilter fileFilterPdfAndImage = new FileFilter() {
public boolean accept(File f) {
if(f.isDirectory()) {
return Lib.getMessage("fileFilterPdfAndImage.Description");
}
};
+ fileChooserSrcFile = new JFileChooser();
fileChooserSrcFile.addChoosableFileFilter(fileFilterPdfAndImage);
fileChooserSrcFile.setFileFilter(fileFilterPdfAndImage);
+ fileChooserOutputFile = new JFileChooser();
+
String menuOSName;
int menuMask;
switch(Lib.getOS()) {
}
});
JMenuItem menuItemOpenFile = new JMenuItem();
- menuItemOpenFile.setText(Lib.getMessage("menuItemOpenFile.Text" + menuOSName));
+ menuItemOpenFile.setText(Lib.getMessage("menuItemOpenFile.Text" + menuOSName) + "...");
menuItemOpenFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, menuMask));
menuItemOpenFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
JMenuItem menuItemOutput = new JMenuItem();
- menuItemOutput.setText(Lib.getMessage("menuItemOutput.Text" + menuOSName));
+ menuItemOutput.setText(Lib.getMessage("menuItemOutput.Text" + menuOSName) + "...");
menuItemOutput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
menuItemOutput_actionPerformed();
textFieldOutputFileName = new JTextField();
textFieldOutputFileName.setColumns(40);
- labelMessage = new JLabel();
+ //labelMessage = new JLabel();
buttonOutput = new JButton();
- buttonOutput.setText(Lib.getMessage("buttonOutput.Text"));
+ buttonOutput.setText(Lib.getMessage("buttonOutput.Text") + "...");
+ buttonOutput.setEnabled(false);
buttonOutput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonOutput_actionPerformed();
JPanel panelOutput = new JPanel();
switch(Lib.getOS()) {
case Lib.OS_MAC:
- panelOutput.setLayout(new FlowLayout(FlowLayout.RIGHT));
- panelOutput.add(labelMessage);
+ panelOutput.setLayout(new FlowLayout(FlowLayout.RIGHT, 15, 15));
+ //panelOutput.add(labelMessage);
panelOutput.add(buttonOutput);
break;
default:
panelOutput.setLayout(new FlowLayout(FlowLayout.LEFT));
panelOutput.add(buttonOutput);
- panelOutput.add(labelMessage);
+ //panelOutput.add(labelMessage);
break;
}
JPanel panelSouth = new JPanel();
panelSouth.setLayout(new BoxLayout(panelSouth, BoxLayout.Y_AXIS));
- panelSouth.add(panelOutputFileName);
+ //panelSouth.add(panelOutputFileName);
panelSouth.add(panelOutput);
this.setJMenuBar(menuBar);