Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.draw9patch.ui;
     18 
     19 import com.android.draw9patch.ui.action.ExitAction;
     20 import com.android.draw9patch.ui.action.OpenAction;
     21 import com.android.draw9patch.ui.action.SaveAction;
     22 import com.android.draw9patch.graphics.GraphicsUtilities;
     23 
     24 import javax.swing.JFrame;
     25 import javax.swing.JMenuBar;
     26 import javax.swing.JMenu;
     27 import javax.swing.JMenuItem;
     28 import javax.swing.ActionMap;
     29 import javax.swing.JFileChooser;
     30 import javax.imageio.ImageIO;
     31 import java.awt.HeadlessException;
     32 import java.awt.image.BufferedImage;
     33 import java.io.File;
     34 import java.util.concurrent.ExecutionException;
     35 
     36 import org.jdesktop.swingworker.SwingWorker;
     37 
     38 public class MainFrame extends JFrame {
     39     private ActionMap actionsMap;
     40     private JMenuItem saveMenuItem;
     41     private ImageEditorPanel imageEditor;
     42 
     43     public MainFrame(String path) throws HeadlessException {
     44         super("Draw 9-patch");
     45 
     46         buildActions();
     47         buildMenuBar();
     48         buildContent();
     49 
     50         if (path == null) {
     51             showOpenFilePanel();
     52         } else {
     53             try {
     54                 File file = new File(path);
     55                 BufferedImage img = GraphicsUtilities.loadCompatibleImage(file.toURI().toURL());
     56                 showImageEditor(img, file.getAbsolutePath());
     57             } catch (Exception ex) {
     58                 showOpenFilePanel();
     59             }
     60         }
     61 
     62         // pack();
     63         setSize(1024, 600);
     64     }
     65 
     66     private void buildActions() {
     67         actionsMap = new ActionMap();
     68         actionsMap.put(OpenAction.ACTION_NAME, new OpenAction(this));
     69         actionsMap.put(SaveAction.ACTION_NAME, new SaveAction(this));
     70         actionsMap.put(ExitAction.ACTION_NAME, new ExitAction(this));
     71     }
     72 
     73     private void buildMenuBar() {
     74         JMenu fileMenu = new JMenu("File");
     75         JMenuItem openMenuItem = new JMenuItem();
     76         saveMenuItem = new JMenuItem();
     77         JMenuItem exitMenuItem = new JMenuItem();
     78 
     79         openMenuItem.setAction(actionsMap.get(OpenAction.ACTION_NAME));
     80         fileMenu.add(openMenuItem);
     81 
     82         saveMenuItem.setAction(actionsMap.get(SaveAction.ACTION_NAME));
     83         saveMenuItem.setEnabled(false);
     84         fileMenu.add(saveMenuItem);
     85 
     86         exitMenuItem.setAction(actionsMap.get(ExitAction.ACTION_NAME));
     87         fileMenu.add(exitMenuItem);
     88 
     89         JMenuBar menuBar = new JMenuBar();
     90         menuBar.add(fileMenu);
     91         setJMenuBar(menuBar);
     92     }
     93 
     94     private void buildContent() {
     95         setContentPane(new GradientPanel());
     96     }
     97 
     98     private void showOpenFilePanel() {
     99         add(new OpenFilePanel(this));
    100     }
    101 
    102     public SwingWorker<?, ?> open(File file) {
    103         if (file == null) {
    104             JFileChooser chooser = new JFileChooser();
    105             chooser.setFileFilter(new PngFileFilter());
    106             int choice = chooser.showOpenDialog(this);
    107             if (choice == JFileChooser.APPROVE_OPTION) {
    108                 return new OpenTask(chooser.getSelectedFile());
    109             } else {
    110                 return null;
    111             }
    112         } else {
    113             return new OpenTask(file);
    114         }
    115     }
    116 
    117     void showImageEditor(BufferedImage image, String name) {
    118         getContentPane().removeAll();
    119         imageEditor = new ImageEditorPanel(this, image, name);
    120         add(imageEditor);
    121         saveMenuItem.setEnabled(true);
    122         validate();
    123         repaint();
    124     }
    125 
    126     public SwingWorker<?, ?> save() {
    127         if (imageEditor == null) {
    128             return null;
    129         }
    130 
    131         File file = imageEditor.chooseSaveFile();
    132         return file != null ? new SaveTask(file) : null;
    133     }
    134 
    135     private class SaveTask extends SwingWorker<Boolean, Void> {
    136         private final File file;
    137 
    138         SaveTask(File file) {
    139             this.file = file;
    140         }
    141 
    142         protected Boolean doInBackground() throws Exception {
    143             try {
    144                 ImageIO.write(imageEditor.getImage(), "PNG", file);
    145             } catch (Exception e) {
    146                 e.printStackTrace();
    147             }
    148             return true;
    149         }
    150     }
    151 
    152     private class OpenTask extends SwingWorker<BufferedImage, Void> {
    153         private final File file;
    154 
    155         OpenTask(File file) {
    156             this.file = file;
    157         }
    158 
    159         protected BufferedImage doInBackground() throws Exception {
    160             return GraphicsUtilities.loadCompatibleImage(file.toURI().toURL());
    161         }
    162 
    163         @Override
    164         protected void done() {
    165             try {
    166                 showImageEditor(get(), file.getAbsolutePath());
    167             } catch (InterruptedException e) {
    168                 e.printStackTrace();
    169             } catch (ExecutionException e) {
    170                 e.printStackTrace();
    171             }
    172         }
    173     }
    174 }
    175