1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 import java.awt.Dimension;
24 import java.awt.Image;
25 import java.io.File;
26 import javax.swing.ImageIcon;
27 import javax.swing.JLabel;
28 import javax.swing.SwingWorker;
29 
30 
31 
32 /*
33  * Simple windows, which should show differences if there are some
34  */
35 
36 public class ConvwatchGUI extends javax.swing.JFrame
37 {
38     private ImageIcon[] m_aImageIcon;
39     private String m_sInifile;
40     private int m_nMaxPages;
41     private int m_nCurrentPage;
42 
43     /** Creates new form ConvwatchGUI
44      * @param args
45      */
46 
47     private ConvwatchGUI(String args[])
48     {
49         if (args.length > 0)
50         {
51             if (args[0].endsWith(".ini"))
52             {
53                 m_sInifile = args[0];
54                 fillImageIconsFromInifile();
55             }
56             else
57             {
58                 fillImageIcons(args);
59             }
60         }
61 
62         String sVersion = System.getProperty("java.version");
63         Float f = Float.valueOf(sVersion.substring(0,3));
64         if (f.floatValue() < (float)1.6)
65         {
66             System.out.println("You need at least Java version 1.6");
67             System.exit(1);
68         }
69 
70         initComponents();
71         jLabelDocumentName.setText("Document: " + m_sInifile);
72     }
73 
74     void fillImageIconsFromInifile()
75     {
76         File aFile = new File(m_sInifile);
77         if (!aFile.exists())
78         {
79             GlobalLogWriter.println("Inifile '" + m_sInifile + "' not found.");
80             printUsage();
81             System.exit(1);
82         }
83 
84         IniFile aIniFile = new IniFile(aFile);
85         int nPages = aIniFile.getIntValue("global", "pages", 0);
86         if (nPages < 1)
87         {
88             System.out.println("No pages found.");
89         }
90         m_nMaxPages = nPages;
91         m_nCurrentPage = 1;
92         fillImageIcons();
93     }
94 
95     private void fillImageIcons()
96     {
97         File aFile = new File(m_sInifile);
98         IniFile aIniFile = new IniFile(aFile);
99         String sSection = "page" + m_nCurrentPage;
100         String[] files = new String[3];
101         files[0] = aIniFile.getValue(sSection, "newgfx"); // current created picture
102         files[1] = aIniFile.getValue(sSection, "oldgfx"); // reference picture
103         files[2] = aIniFile.getValue(sSection, "diffgfx");
104         fillImageIcons(files);
105     }
106 
107     /**
108      * Give 3 file names
109      * @param args
110      */
111     private void fillImageIcons(String args[])
112     {
113         boolean bLoadImages = false;
114         m_aImageIcon = new ImageIcon[3];
115         for (int i=0;i<3;i++)
116         {
117             if (args.length > i && args[i] != null)
118             {
119                 File aFile = new File(args[i]);
120                 if (aFile.exists())
121                 {
122                     // TODO: Load images
123                     // Image aImage = new BufferedImage(100,100, BufferedImage.TYPE_INT_RGB);
124                     // aImage.
125                     m_aImageIcon[i] = new ImageIcon(args[i]);
126                     if (m_aImageIcon[i] != null)
127                     {
128                         bLoadImages = true;
129                     }
130                 }
131                 else
132                 {
133                     System.out.println("Can't read file: " + aFile.getName());
134                     bLoadImages = false;
135                 }
136             }
137             else
138             {
139                 System.out.println("There is no #" + (i + 1) + " image given.");
140                 bLoadImages = false;
141             }
142         }
143 //        if (!bLoadImages)
144 //        {
145 //            printUsage();
146 //            System.exit(1);
147 //        }
148 
149 
150 
151         // TODO: Set images.
152 
153         // formComponentResized(null);
154     }
155 
156     private void printUsage()
157     {
158         System.out.println("Usage:");
159         System.out.println("   ConvwatchGUI <pic1> <pic2> <pic3>");
160         System.out.println("or ConvwatchGUI <inifile>");
161     }
162 
163 //    private int m_nOldWidth;
164 
165     /** This method is called from within the constructor to
166      * initialize the form.
167      * WARNING: Do NOT modify this code. The content of this method is
168      * always regenerated by the Form Editor.
169      */
170     @SuppressWarnings("unchecked")
171     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
172     private void initComponents() {
173 
174         jPanelOriginal = new javax.swing.JPanel();
175         jLabelOriginalImage = new javax.swing.JLabel();
176         jPanelReference = new javax.swing.JPanel();
177         jLabelReferenceImage = new javax.swing.JLabel();
178         jPanelDifference = new javax.swing.JPanel();
179         jLabelDifferenceImage = new javax.swing.JLabel();
180         jPanel4 = new javax.swing.JPanel();
181         jButton1 = new javax.swing.JButton();
182         jButton2 = new javax.swing.JButton();
183         jButton3 = new javax.swing.JButton();
184         jLabelCurrentPage = new javax.swing.JLabel();
185         jPanel5 = new javax.swing.JPanel();
186         jLabel1 = new javax.swing.JLabel();
187         jLabelDocumentName = new javax.swing.JLabel();
188 
189         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
190         addComponentListener(new java.awt.event.ComponentAdapter() {
191             public void componentResized(java.awt.event.ComponentEvent evt) {
192                 formComponentResized(evt);
193             }
194         });
195         addPropertyChangeListener(new java.beans.PropertyChangeListener() {
196             public void propertyChange(java.beans.PropertyChangeEvent evt) {
197                 formPropertyChange(evt);
198             }
199         });
200 
201         jPanelOriginal.setBorder(javax.swing.BorderFactory.createTitledBorder("Picture"));
202 
203         javax.swing.GroupLayout jPanelOriginalLayout = new javax.swing.GroupLayout(jPanelOriginal);
204         jPanelOriginal.setLayout(jPanelOriginalLayout);
205         jPanelOriginalLayout.setHorizontalGroup(
206             jPanelOriginalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
207             .addGroup(jPanelOriginalLayout.createSequentialGroup()
208                 .addContainerGap()
209                 .addComponent(jLabelOriginalImage, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
210                 .addContainerGap())
211         );
212         jPanelOriginalLayout.setVerticalGroup(
213             jPanelOriginalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
214             .addGroup(jPanelOriginalLayout.createSequentialGroup()
215                 .addComponent(jLabelOriginalImage, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
216                 .addContainerGap())
217         );
218 
219         jPanelReference.setBorder(javax.swing.BorderFactory.createTitledBorder("Reference Picture"));
220 
221         javax.swing.GroupLayout jPanelReferenceLayout = new javax.swing.GroupLayout(jPanelReference);
222         jPanelReference.setLayout(jPanelReferenceLayout);
223         jPanelReferenceLayout.setHorizontalGroup(
224             jPanelReferenceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
225             .addGroup(jPanelReferenceLayout.createSequentialGroup()
226                 .addContainerGap()
227                 .addComponent(jLabelReferenceImage, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
228                 .addContainerGap())
229         );
230         jPanelReferenceLayout.setVerticalGroup(
231             jPanelReferenceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
232             .addGroup(jPanelReferenceLayout.createSequentialGroup()
233                 .addComponent(jLabelReferenceImage, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
234                 .addContainerGap())
235         );
236 
237         jPanelDifference.setBorder(javax.swing.BorderFactory.createTitledBorder("Difference"));
238 
239         jLabelDifferenceImage.setName("DifferenceImage"); // NOI18N
240 
241         javax.swing.GroupLayout jPanelDifferenceLayout = new javax.swing.GroupLayout(jPanelDifference);
242         jPanelDifference.setLayout(jPanelDifferenceLayout);
243         jPanelDifferenceLayout.setHorizontalGroup(
244             jPanelDifferenceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
245             .addGroup(jPanelDifferenceLayout.createSequentialGroup()
246                 .addContainerGap()
247                 .addComponent(jLabelDifferenceImage, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
248                 .addContainerGap())
249         );
250         jPanelDifferenceLayout.setVerticalGroup(
251             jPanelDifferenceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
252             .addGroup(jPanelDifferenceLayout.createSequentialGroup()
253                 .addComponent(jLabelDifferenceImage, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
254                 .addContainerGap())
255         );
256 
257         jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Action"));
258 
259         jButton1.setText("Close");
260         jButton1.addActionListener(new java.awt.event.ActionListener() {
261             public void actionPerformed(java.awt.event.ActionEvent evt) {
262                 jButton1ActionPerformed(evt);
263             }
264         });
265 
266         jButton2.setLabel("prev page");
267         jButton2.addActionListener(new java.awt.event.ActionListener() {
268             public void actionPerformed(java.awt.event.ActionEvent evt) {
269                 jButton2ActionPerformed(evt);
270             }
271         });
272 
273         jButton3.setLabel("next page");
274         jButton3.addActionListener(new java.awt.event.ActionListener() {
275             public void actionPerformed(java.awt.event.ActionEvent evt) {
276                 jButton3ActionPerformed(evt);
277             }
278         });
279 
280         jLabelCurrentPage.setText("Current page: 1");
281 
282         javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
283         jPanel4.setLayout(jPanel4Layout);
284         jPanel4Layout.setHorizontalGroup(
285             jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
286             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
287                 .addContainerGap()
288                 .addComponent(jButton2)
289                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
290                 .addComponent(jButton3)
291                 .addGap(18, 18, 18)
292                 .addComponent(jLabelCurrentPage, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
293                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 614, Short.MAX_VALUE)
294                 .addComponent(jButton1)
295                 .addContainerGap())
296         );
297         jPanel4Layout.setVerticalGroup(
298             jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
299             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
300                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
301                 .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
302                     .addComponent(jButton1)
303                     .addComponent(jButton2)
304                     .addComponent(jButton3)
305                     .addComponent(jLabelCurrentPage))
306                 .addContainerGap())
307         );
308 
309         jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Information"));
310 
311         jLabel1.setText("<html>Here you see a graphical compare by pictures created with a current running office, a stored reference picture and the difference between those both pictures created by ImageMagicks 'composite'.\n</html>"); // NOI18N
312 
313         jLabelDocumentName.setText("jLabel2");
314 
315         javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
316         jPanel5.setLayout(jPanel5Layout);
317         jPanel5Layout.setHorizontalGroup(
318             jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
319             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
320                 .addContainerGap()
321                 .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
322                     .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 972, Short.MAX_VALUE)
323                     .addComponent(jLabelDocumentName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 972, Short.MAX_VALUE))
324                 .addContainerGap())
325         );
326         jPanel5Layout.setVerticalGroup(
327             jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
328             .addGroup(jPanel5Layout.createSequentialGroup()
329                 .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
330                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
331                 .addComponent(jLabelDocumentName, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
332                 .addContainerGap())
333         );
334 
335         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
336         getContentPane().setLayout(layout);
337         layout.setHorizontalGroup(
338             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
339             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
340                 .addContainerGap()
341                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
342                     .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
343                     .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
344                     .addGroup(layout.createSequentialGroup()
345                         .addComponent(jPanelOriginal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
346                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
347                         .addComponent(jPanelReference, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
348                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
349                         .addComponent(jPanelDifference, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
350                 .addContainerGap())
351         );
352         layout.setVerticalGroup(
353             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
354             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
355                 .addContainerGap()
356                 .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
357                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
358                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
359                     .addComponent(jPanelReference, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
360                     .addComponent(jPanelOriginal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
361                     .addComponent(jPanelDifference, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
362                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
363                 .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
364                 .addContainerGap())
365         );
366 
367         pack();
368     }// </editor-fold>//GEN-END:initComponents
369 
370     private boolean bAdd = false;
371 
372     private void formComponentResized(java.awt.event.ComponentEvent evt)//GEN-FIRST:event_formComponentResized
373     {//GEN-HEADEREND:event_formComponentResized
374         // TODO add your handling code here:
375         // we need to set icons to null
376         // if we don't do this, icons can only grow, but not shrink :-(
377 
378         initialiseImages();
379     }
380     private void initialiseImages()
381     {
382         if (jLabelOriginalImage.getIcon() != null)
383         {
384             jLabelOriginalImage.setIcon(null);
385             jLabelReferenceImage.setIcon(null);
386             jLabelDifferenceImage.setIcon(null);
387 
388             int w = getWidth();
389             int h = getHeight();
390             if (bAdd)
391             {
392                 this.setSize(w, h + 1);
393                 bAdd = false;
394             }
395             else
396             {
397                 this.setSize(w, h - 1);
398                 bAdd = true;
399             }
400         }
401         else
402         {
403             new ResizeImage(jLabelOriginalImage, m_aImageIcon[0]).execute();
404             new ResizeImage(jLabelReferenceImage, m_aImageIcon[1]).execute();
405             new ResizeImage(jLabelDifferenceImage, m_aImageIcon[2]).execute();
406         }
407         int dummy=0;
408     }//GEN-LAST:event_formComponentResized
409 
410     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
411     {//GEN-HEADEREND:event_jButton1ActionPerformed
412         // TODO add your handling code here:
413         System.exit(1);
414     }//GEN-LAST:event_jButton1ActionPerformed
415 
416     private void formPropertyChange(java.beans.PropertyChangeEvent evt)//GEN-FIRST:event_formPropertyChange
417     {//GEN-HEADEREND:event_formPropertyChange
418         // TODO add your handling code here:
419         int dummy = 0;
420     }//GEN-LAST:event_formPropertyChange
421 
422     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton2ActionPerformed
423     {//GEN-HEADEREND:event_jButton2ActionPerformed
424         // TODO add your handling code here:
425         int nOldPage = m_nCurrentPage;
426         if (m_nCurrentPage > 1)
427         {
428             m_nCurrentPage--;
429         }
430         if (nOldPage != m_nCurrentPage)
431         {
432             jLabelCurrentPage.setText("Current page: " + m_nCurrentPage);
433             fillImageIcons();
434             initialiseImages();
435         }
436     }//GEN-LAST:event_jButton2ActionPerformed
437 
438     private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton3ActionPerformed
439     {//GEN-HEADEREND:event_jButton3ActionPerformed
440         // TODO add your handling code here:
441         int nOldPage = m_nCurrentPage;
442         if (m_nCurrentPage < m_nMaxPages)
443         {
444             m_nCurrentPage++;
445         }
446         if (nOldPage != m_nCurrentPage)
447         {
448             jLabelCurrentPage.setText("Current page: " + m_nCurrentPage);
449             fillImageIcons();
450             initialiseImages();
451         }
452     }//GEN-LAST:event_jButton3ActionPerformed
453 
454     class ResizeImage extends SwingWorker <ImageIcon, Object>
455     {
456         private JLabel m_jLabel;
457         private ImageIcon m_aImageIcon;
458         private int w;
459         private int h;
460 
461         public ResizeImage(JLabel _aLabel, ImageIcon _aImageIcon)
462         {
463             m_jLabel = _aLabel;
464             m_aImageIcon = _aImageIcon;
465             w = _aLabel.getWidth();
466             h = _aLabel.getHeight();
467         }
468 
469         // dont access here anything to "Event Swing Thread"
470         @Override
471         public ImageIcon doInBackground()
472         {
473             Image aImage = m_aImageIcon.getImage().getScaledInstance(w, h, Image.SCALE_AREA_AVERAGING); // SCALE_SMOOTH
474             final ImageIcon aIcon = new ImageIcon(aImage);
475         // m_jLabel.setIcon(aIcon);
476             return aIcon;
477         }
478 
479         @Override
480         protected void done()
481         {
482             try
483             {
484                 m_jLabel.setIcon(get());
485             }
486             catch (Exception e)
487             {}
488         }
489 
490     }
491 
492     /**
493      * @param args the command line arguments
494      */
495     public static void main(final String args[])
496     {
497 
498         // Start GUI
499 
500         java.awt.EventQueue.invokeLater(new Runnable()
501         {
502 
503             public void run()
504             {
505                 ConvwatchGUI aGUI = new ConvwatchGUI(args);
506                 aGUI.setTitle("Graphical Compare");
507                 aGUI.setPreferredSize(new Dimension(1024, 768));
508 
509                 aGUI.setVisible(true);
510             }
511         });
512     }
513     // Variables declaration - do not modify//GEN-BEGIN:variables
514     private javax.swing.JButton jButton1;
515     private javax.swing.JButton jButton2;
516     private javax.swing.JButton jButton3;
517     private javax.swing.JLabel jLabel1;
518     private javax.swing.JLabel jLabelCurrentPage;
519     private javax.swing.JLabel jLabelDifferenceImage;
520     private javax.swing.JLabel jLabelDocumentName;
521     private javax.swing.JLabel jLabelOriginalImage;
522     private javax.swing.JLabel jLabelReferenceImage;
523     private javax.swing.JPanel jPanel4;
524     private javax.swing.JPanel jPanel5;
525     private javax.swing.JPanel jPanelDifference;
526     private javax.swing.JPanel jPanelOriginal;
527     private javax.swing.JPanel jPanelReference;
528     // End of variables declaration//GEN-END:variables
529 }
530