diff --git a/importPicture/src/osm/jp/gpx/ImportPicture.java b/importPicture/src/osm/jp/gpx/ImportPicture.java index 63668a3..afdf02b 100644 --- a/importPicture/src/osm/jp/gpx/ImportPicture.java +++ b/importPicture/src/osm/jp/gpx/ImportPicture.java @@ -19,6 +19,7 @@ import javax.xml.parsers.*; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; @@ -70,7 +71,7 @@ finally { try { if (inStream != null) { - inStream.close(); + inStream.close(); } } catch (IOException e) { logger.warning("ログ設定: ログ設定プロパティファイルのストリームクローズ時に例外が発生しました。:"+ e.toString()); @@ -157,7 +158,7 @@ obj.start(); try { obj.join(); - } catch(InterruptedException e) {} + } catch(InterruptedException end) {} } public File gpxDir = new File("."); @@ -274,8 +275,7 @@ transformer.transform(source, result); } } - catch(Exception e) { - e.printStackTrace(); + catch(ParserConfigurationException | DOMException | SAXException | IOException | ParseException | ImageReadException | ImageWriteException | IllegalArgumentException | TransformerException e) { } } @@ -305,17 +305,12 @@ if ((uktime.getTime() >= gpxStartTime) && (uktime.getTime() <= gpxEndTime)) { Element trkpt = trkpt(map, uktime); if (trkpt != null) { - System.out.print(String.format("%12s|", image.getName())); - System.out.print(String.format("%20s ", dfjp.format(itime))); - System.out.print(String.format("%20s|", dfjp.format(uktime))); - Element wpt = createWptTag(image, uktime.getTime(), trkpt); String latStr = wpt.getAttribute("lat"); String lonStr = wpt.getAttribute("lon"); - System.out.print(String.format("%12s %12s|", latStr, lonStr)); double latitude = Double.parseDouble(latStr); double longitude = Double.parseDouble(lonStr); - + String eleStr = "-"; String magvarStr = "-"; NodeList nodes = wpt.getChildNodes(); // 子ノードを取得 @@ -332,10 +327,15 @@ } } } - System.out.println(String.format("%8s|%6s|", eleStr, magvarStr)); - try { - Thread.sleep(1); - } catch(InterruptedException e) {} + + synchronized(System.out) { + System.out.print(String.format("%12s|", image.getName())); + System.out.print(String.format("%20s ", dfjp.format(itime))); + System.out.print(String.format("%20s|", dfjp.format(uktime))); + System.out.print(String.format("%12s %12s|", latStr, lonStr)); + System.out.println(String.format("%8s|%6s|", eleStr, magvarStr)); + } + Thread.yield(); if (exifWrite) { TiffOutputSet outputSet = null; diff --git a/importPicture/src/osm/jp/gpx/matchtime/gui/Command.java b/importPicture/src/osm/jp/gpx/matchtime/gui/Command.java index 93f1e1e..7b8f617 100644 --- a/importPicture/src/osm/jp/gpx/matchtime/gui/Command.java +++ b/importPicture/src/osm/jp/gpx/matchtime/gui/Command.java @@ -35,6 +35,7 @@ } @SuppressWarnings("unchecked") + @Override public void run() { System.out.println("[START:"+ (new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss")).format(new java.util.Date()) +"]\t"+ this.commandName); for (int i=0; i < args.length; i++) { @@ -53,16 +54,15 @@ } catch (InvocationTargetException e) { System.out.println("[ERR!:"+ (new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss")).format(new java.util.Date()) +"]\t"+ this.commandName); - e.getCause().printStackTrace(); throw e; } - catch (Exception e) { + catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException e) { System.out.println("[ERR!:"+ (new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss")).format(new java.util.Date()) +"]\t"+ this.commandName); throw e; } } - catch(Exception e) { - e.printStackTrace(); + catch(InvocationTargetException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException e) { + e.printStackTrace(System.out); } System.out.println(); } diff --git a/importPicture/src/osm/jp/gpx/matchtime/gui/DoDialog.java b/importPicture/src/osm/jp/gpx/matchtime/gui/DoDialog.java index 6e6f634..8e2e69e 100644 --- a/importPicture/src/osm/jp/gpx/matchtime/gui/DoDialog.java +++ b/importPicture/src/osm/jp/gpx/matchtime/gui/DoDialog.java @@ -1,5 +1,11 @@ package osm.jp.gpx.matchtime.gui; -import java.awt.*; +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.Container; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.Point; +import java.awt.event.ActionEvent; import java.io.*; import javax.swing.*; @@ -22,18 +28,6 @@ JTextArea textArea; // 実行結果を表示するJTextArea (中央) //}} - class SymAction implements java.awt.event.ActionListener { - public void actionPerformed(java.awt.event.ActionEvent event) { - Object object = event.getSource(); - if (object == closeButton) { - closeButton_Action(event); - } - else if (object == doButton) { - doButton_Action(event); - } - } - } - public DoDialog(JFrame parentFrame, String[] args) { super(parentFrame, true); // モーダルダイアログを基盤にする this.parentFrame = parentFrame; @@ -42,10 +36,9 @@ // INIT_CONTROLS Container container = getContentPane(); container.setLayout(new BorderLayout()); - setVisible(false); - setSize(getInsets().left + getInsets().right + 500,getInsets().top + getInsets().bottom + 480); + parentFrame.setVisible(false); + setSize(getInsets().left + getInsets().right + 980, getInsets().top + getInsets().bottom + 480); setTitle(DoDialog.TITLE); - setSize(500,480); // コントロールパネル buttonPanel = new JPanel(); @@ -53,10 +46,23 @@ doButton = new JButton("実行"); doButton.setToolTipText("処理を実行します."); doButton.setEnabled(true); + doButton.addActionListener((ActionEvent event) -> { + // 処理中であることを示すため + // ボタンの文字列を変更し,使用不可にする + doButton.setText("処理中..."); + doButton.setEnabled(false); + + // SwingWorker を生成し,実行する + LongTaskWorker worker = new LongTaskWorker(doButton); + worker.execute(); + }); buttonPanel.add(doButton); closeButton = new JButton("閉じる"); closeButton.setToolTipText("処理を終了します."); + closeButton.addActionListener((ActionEvent event) -> { + dispose(); + }); buttonPanel.add(closeButton); this.getContentPane().add("South", buttonPanel); @@ -66,30 +72,29 @@ JScrollPane sc=new JScrollPane(textArea); textArea.setFont(new Font(Font.MONOSPACED,Font.PLAIN,12)); textArea.setTabSize(4); + this.getContentPane().add("Center", sc); + try { textArea.append("> java -cp importPicture.jar osm.jp.gpx.ImportPicture"); - for (int i = 0; i < args.length; i++) { - textArea.append(" '"+ args[i] +"'"); + for (String arg : args) { + textArea.append(" '" + arg + "'"); } textArea.append("\n\n"); } catch (Exception e) { System.out.println(e.toString()); } - this.getContentPane().add("Center", sc); - //{{REGISTER_LISTENERS - SymAction lSymAction = new SymAction(); - closeButton.addActionListener(lSymAction); - doButton.addActionListener(lSymAction); - //}} + // JFrameの表示 + parentFrame.setVisible(true); } - + /** * Shows or hides the component depending on the boolean flag b. * @param b trueのときコンポーネントを表示; その他のとき, componentを隠す. * @see java.awt.Component#isVisible */ + @Override public void setVisible(boolean b) { if(b) { setLocation(80, 80); @@ -97,6 +102,7 @@ super.setVisible(b); } + @Override public void addNotify() { // Record the size of the window prior to calling parents addNotify. Dimension d = getSize(); @@ -109,56 +115,110 @@ // Adjust components according to the insets setSize(getInsets().left + getInsets().right + d.width, getInsets().top + getInsets().bottom + d.height); Component components[] = getComponents(); - for (int i = 0; i < components.length; i++) { - Point p = components[i].getLocation(); + for (Component component : components) { + Point p = component.getLocation(); p.translate(getInsets().left, getInsets().top); - components[i].setLocation(p); + component.setLocation(p); } fComponentsAdjusted = true; } + /** - * [実行]ボタンをクリックしたときの動作 - * @param event + * JTextAreaに書き出すOutputStream */ - void doButton_Action(java.awt.event.ActionEvent event) { - doButton.setEnabled(false); + public static class JTextAreaOutputStream extends OutputStream { + private ByteArrayOutputStream os; - PrintStream defOut = System.out; - PrintStream defErr = System.err; + /** 書き出し対象 */ + private JTextArea textArea; + private String encode; - ByteArrayOutputStream stdout = new ByteArrayOutputStream(); - try { - System.setOut(new PrintStream(stdout)); - System.setErr(new PrintStream(stdout)); - - Command command = new Command(osm.jp.gpx.ImportPicture.class); - command.setArgs(args); - command.start(); // コマンドを実行 - while (command.isAlive()) { - Thread.sleep(1000); - textArea.append(stdout.toString()); - stdout.reset(); - } - textArea.append(stdout.toString()); - JOptionPane.showMessageDialog(this, "'"+ TITLE +"'処理を完了しました。", "処理完了", JOptionPane.INFORMATION_MESSAGE); + public JTextAreaOutputStream(JTextArea textArea, String encode) { + this.textArea = textArea; + this.encode = encode; + this.os = new ByteArrayOutputStream(); } - catch(Exception e) { - e.printStackTrace(); - JOptionPane.showMessageDialog(this, e.toString(), "Exception", JOptionPane.ERROR_MESSAGE); + + /** + * OutputStream#write(byte[])のオーバーライド + * @param arg + */ + @Override + public void write(int arg) throws IOException { + this.os.write(arg); } - finally { - System.setOut(defOut); - System.setErr(defErr); - doButton.setEnabled(true); + + /** + * flush()でJTextAreaに書き出す + */ + @Override + public void flush() throws IOException { + // 文字列のエンコード + final String str = new String(this.os.toByteArray(), this.encode); + // 実際の書き出し処理 + SwingUtilities.invokeLater(new Runnable(){ + @Override + public void run() { + JTextAreaOutputStream.this.textArea.append(str); + } + }); + // 書き出した内容はクリアする + this.os.reset(); } } - - void closeButton_Action(java.awt.event.ActionEvent event) { - dispose(); - } - void changeSQL_Action(java.awt.event.ActionEvent event) { - textArea.setText(""); + // 非同期に行う処理を記述するためのクラス + class LongTaskWorker extends SwingWorker { + private JButton button; + + public LongTaskWorker(JButton button) { + this.button = button; + } + + // 非同期に行われる処理 + @Override + public Object doInBackground() { + // ながーい処理 + PrintStream defOut = System.out; + PrintStream defErr = System.err; + + OutputStream os = new JTextAreaOutputStream(textArea, "UTF-8"); + PrintStream stdout = new PrintStream(os, true); // 自動flushをtrueにしておく + + // System.out にJTextAreaOutputStreamに書き出すPrintStreamを設定 + System.setOut(stdout); + System.setErr(stdout); + + try { + Command command = new Command(osm.jp.gpx.ImportPicture.class); + command.setArgs(args); + command.start(); // コマンドを実行 + while (command.isAlive()) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) {} + } + } + catch(Exception e) { + e.printStackTrace(stdout); + } + finally { + System.setOut(defOut); + System.setErr(defErr); + doButton.setEnabled(true); + } + + return null; + } + + // 非同期処理後に実行 + @Override + protected void done() { + // 処理が終了したので,文字列を元に戻し + // ボタンを使用可能にする + button.setText("実行"); + button.setEnabled(true); + } } }