View Javadoc

1   package org.semwebcentral.gforge.theme.tabs;
2   
3   /***
4    * Created by IntelliJ IDEA.
5    * User: ryan
6    * Date: Jan 12, 2004
7    * Time: 10:39:28 PM
8    * To change this template use Options | File Templates.
9    */
10  public class TabGenerator
11  {
12  
13      public static final String configLoc = "/Users/ryan/dev/projects/semwebcentral/src/webapp/resources/tab-conf-semwebcentral.xml";
14  
15      public static final String tabSheetLoc = "/Users/ryan/dev/projects/semwebcentral/src/webapp/assets/images/forge/tab-sheet-semwebcentral.png";
16  
17      public static void main(String[] args) throws InterruptedException, java.io.IOException
18      {
19          try
20          {
21              //generateStarterDoc();
22              init();
23              //testClip(tabSheetLoc);
24          }
25          catch (java.io.FileNotFoundException e)
26          {
27              System.out.println(e);  //To change body of catch statement use Options | File Templates.
28              e.printStackTrace();
29          }
30      }
31  
32      private static void init() throws InterruptedException, java.io.IOException
33      {
34          TabCellConfig config = loadTabCellConfig();
35          System.out.println("config=");
36          System.out.println(config);
37          java.awt.Image sourceImage = GraphicUtil.loadImage(tabSheetLoc);
38          String outputDir = getOutputDir(tabSheetLoc);
39          genTabRow(config.getTopCellRow(), sourceImage, outputDir);
40          genTabRow(config.getBottomCellRow(), sourceImage, outputDir);
41      }
42  
43      private static void genTabRow(TabCellRow row, java.awt.Image sourceImage, String outputDir) throws InterruptedException, java.io.IOException
44      {
45          xLoc = 0;
46          for (java.util.Iterator it = row.iterator(); it.hasNext();)
47          {
48              TabCell cell = (TabCell) it.next();
49              saveClip(cell, sourceImage, outputDir);
50          }
51          yLoc += row.getTabCell(0).getActualHeight();
52      }
53  
54      private static String getOutputDir(String tabsheetloc) throws java.io.FileNotFoundException
55      {
56          java.io.File file = new java.io.File(tabsheetloc);
57          if (!file.exists())
58          {
59              throw new java.io.FileNotFoundException(tabsheetloc);
60          }
61          return  file.getParentFile().getPath() + "/gentabs/";
62      }
63  
64      private static int xLoc = 0;
65      private static int yLoc = 0;
66  
67      private static void saveClip(TabCell cell, java.awt.Image image, String outputDir) throws java.io.IOException
68      {
69          int width = cell.getActualWidth();
70          int height = cell.getActualHeight();
71  
72          System.out.println("\ngen: " + xLoc + "," + yLoc + "," + width + "," + height);
73  
74          java.awt.image.BufferedImage clipImage = GraphicUtil.clipImage(image, xLoc, yLoc, width, height);
75          GraphicUtil.saveAsPng(outputDir, cell.getActualImage(), clipImage);
76          xLoc += width;
77          //yLoc += height;
78      }
79  
80      private static void generateStarterDoc()
81      {
82          TabCellRow list = new TabCellRow();
83  
84          TabCell cell;
85  
86          cell = new TabCell();
87          cell.setBgImage("leftselect.png");
88          cell.setBgHeight("23");
89          cell.setBgWidth("5");
90          list.setTabCell(0, cell);
91  
92          cell = new TabCell();
93          cell.setBgImage("select.png");
94          cell.setBgHeight("23");
95          cell.setBgWidth("16%");
96          cell.setBgAlign("CENTER");
97          list.setTabCell(1, cell);
98  
99          cell = new TabCell();
100         cell.setBgImage("rightselect.png");
101         cell.setBgHeight("23");
102         cell.setBgWidth("5");
103         list.setTabCell(2, cell);
104 
105         TabCellConfig config = new TabCellConfig();
106         config.setTopCellRow(list);
107         config.setBottomCellRow(list);
108 
109         try
110         {
111             org.semwebcentral.util.CastorUtil.save(configLoc, config);
112         }
113         catch (java.io.IOException e)
114         {
115             System.out.println(e);  //To change body of catch statement use Options | File Templates.
116         }
117         catch (org.exolab.castor.xml.MarshalException e)
118         {
119             System.out.println(e);  //To change body of catch statement use Options | File Templates.
120         }
121         catch (org.exolab.castor.xml.ValidationException e)
122         {
123             System.out.println(e);  //To change body of catch statement use Options | File Templates.
124         }
125     }
126 
127     public static void testResize(String imageLocation) throws java.io.FileNotFoundException
128     {
129         java.io.File file = new java.io.File(imageLocation);
130         if (!file.exists())
131         {
132             throw new java.io.FileNotFoundException(imageLocation);
133         }
134         String outputDir = file.getParentFile().getPath() + "/gentabs/";
135 
136         try
137         {
138             java.awt.Image image = GraphicUtil.loadImage(imageLocation);
139             //int imageWidth = image.getWidth(null);
140             //int imageHeight = image.getHeight(null);
141             java.awt.image.BufferedImage thumbImage = GraphicUtil.resizeImage(image, 300, 10);
142             String fileName = "test-resize";
143             //saveAsJpg(outputDir, fileName, thumbImage);
144             GraphicUtil.saveAsPng(outputDir, fileName, thumbImage);
145         }
146         catch (InterruptedException e)
147         {
148             System.out.println(e);  //To change body of catch statement use Options | File Templates.
149         }
150         catch (java.io.IOException e)
151         {
152             System.out.println(e);  //To change body of catch statement use Options | File Templates.
153         }
154     }
155 
156     public static void testClip(String imageLocation) throws java.io.FileNotFoundException
157     {
158         java.io.File file = new java.io.File(imageLocation);
159         if (!file.exists())
160         {
161             throw new java.io.FileNotFoundException(imageLocation);
162         }
163         String outputDir = file.getParentFile().getPath() + "/gentabs/";
164 
165         try
166         {
167             java.awt.Image image = GraphicUtil.loadImage(imageLocation);
168             //int imageWidth = image.getWidth(null);
169             //int imageHeight = image.getHeight(null);
170             java.awt.image.BufferedImage thumbImage = GraphicUtil.clipImage(image, 14, 23, 30, 8);
171             String fileName = "test-clip";
172             //saveAsJpg(outputDir, fileName, thumbImage);
173             GraphicUtil.saveAsPng(outputDir, fileName, thumbImage);
174         }
175         catch (InterruptedException e)
176         {
177             System.out.println(e);  //To change body of catch statement use Options | File Templates.
178         }
179         catch (java.io.IOException e)
180         {
181             System.out.println(e);  //To change body of catch statement use Options | File Templates.
182         }
183         System.out.println("DONE.");
184     }
185 
186     private static TabCellConfig loadTabCellConfig()
187     {
188         TabCellConfig config = null;
189         try
190         {
191             Object o = org.semwebcentral.util.CastorUtil.load(TabCellConfig.class, configLoc);
192             config = (TabCellConfig) o;
193         }
194         catch (java.io.IOException e)
195         {
196             System.out.println(e);  //To change body of catch statement use Options | File Templates.
197         }
198         catch (org.exolab.castor.xml.MarshalException e)
199         {
200             System.out.println(e);  //To change body of catch statement use Options | File Templates.
201         }
202         catch (org.exolab.castor.xml.ValidationException e)
203         {
204             System.out.println(e);  //To change body of catch statement use Options | File Templates.
205         }
206         catch (ClassCastException e)
207         {
208             System.out.println(e);  //To change body of catch statement use Options | File Templates.
209             System.out.println(e.getMessage());  //To change body of catch statement use Options | File Templates.
210             e.printStackTrace();
211         }
212         return config;
213     }
214 
215 }