auto import from //depot/cupcake/@135843

This commit is contained in:
The Android Open Source Project
2009-03-03 19:29:09 -08:00
parent d4aee0c0ca
commit 52d4c30ca5
2386 changed files with 299112 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.draw9patch;
import com.android.draw9patch.ui.MainFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Application {
private static void initUserInterface() {
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Draw 9-patch");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
public static void main(String... args) {
initUserInterface();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainFrame frame = new MainFrame();
frame.setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

View File

@@ -0,0 +1,96 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.draw9patch.graphics;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Graphics;
import java.awt.Transparency;
import java.net.URL;
import java.io.IOException;
public class GraphicsUtilities {
public static BufferedImage loadCompatibleImage(URL resource) throws IOException {
BufferedImage image = ImageIO.read(resource);
return toCompatibleImage(image);
}
public static BufferedImage createCompatibleImage(int width, int height) {
return getGraphicsConfiguration().createCompatibleImage(width, height);
}
public static BufferedImage toCompatibleImage(BufferedImage image) {
if (isHeadless()) {
return image;
}
if (image.getColorModel().equals(getGraphicsConfiguration().getColorModel())) {
return image;
}
BufferedImage compatibleImage = getGraphicsConfiguration().createCompatibleImage(
image.getWidth(), image.getHeight(), image.getTransparency());
Graphics g = compatibleImage.getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return compatibleImage;
}
public static BufferedImage createCompatibleImage(BufferedImage image, int width, int height) {
return getGraphicsConfiguration().createCompatibleImage(width, height,
image.getTransparency());
}
private static GraphicsConfiguration getGraphicsConfiguration() {
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
return environment.getDefaultScreenDevice().getDefaultConfiguration();
}
private static boolean isHeadless() {
return GraphicsEnvironment.isHeadless();
}
public static BufferedImage createTranslucentCompatibleImage(int width, int height) {
return getGraphicsConfiguration().createCompatibleImage(width, height,
Transparency.TRANSLUCENT);
}
public static int[] getPixels(BufferedImage img, int x, int y, int w, int h, int[] pixels) {
if (w == 0 || h == 0) {
return new int[0];
}
if (pixels == null) {
pixels = new int[w * h];
} else if (pixels.length < w * h) {
throw new IllegalArgumentException("Pixels array must have a length >= w * h");
}
int imageType = img.getType();
if (imageType == BufferedImage.TYPE_INT_ARGB || imageType == BufferedImage.TYPE_INT_RGB) {
Raster raster = img.getRaster();
return (int[]) raster.getDataElements(x, y, w, h, pixels);
}
// Unmanages the image
return img.getRGB(x, y, w, h, pixels, 0, w);
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.draw9patch.ui;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.GradientPaint;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.BorderLayout;
import javax.swing.JPanel;
class GradientPanel extends JPanel {
private static final int DARK_BLUE = 0x202737;
GradientPanel() {
super(new BorderLayout());
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Rectangle clip = g2.getClipBounds();
Paint paint = g2.getPaint();
g2.setPaint(new GradientPaint(0.0f, getHeight() * 0.22f, new Color(DARK_BLUE),
0.0f, getHeight() * 0.9f, Color.BLACK));
g2.fillRect(clip.x, clip.y, clip.width, clip.height);
g2.setPaint(paint);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,63 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.draw9patch.ui;
import javax.swing.TransferHandler;
import javax.swing.JComponent;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.net.MalformedURLException;
class ImageTransferHandler extends TransferHandler {
private final MainFrame mainFrame;
ImageTransferHandler(MainFrame mainFrame) {
this.mainFrame = mainFrame;
}
@Override
public boolean importData(JComponent component, Transferable transferable) {
try {
Object data = transferable.getTransferData(DataFlavor.javaFileListFlavor);
//noinspection unchecked
final File file = ((List<File>) data).get(0);
mainFrame.open(file).execute();
} catch (UnsupportedFlavorException e) {
return false;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
@Override
public boolean canImport(JComponent component, DataFlavor[] dataFlavors) {
for (DataFlavor flavor : dataFlavors) {
if (flavor.isFlavorJavaFileListType()) {
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,164 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.draw9patch.ui;
import com.android.draw9patch.ui.action.ExitAction;
import com.android.draw9patch.ui.action.OpenAction;
import com.android.draw9patch.ui.action.SaveAction;
import com.android.draw9patch.graphics.GraphicsUtilities;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.ActionMap;
import javax.swing.JFileChooser;
import javax.imageio.ImageIO;
import java.awt.HeadlessException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.concurrent.ExecutionException;
import org.jdesktop.swingworker.SwingWorker;
public class MainFrame extends JFrame {
private ActionMap actionsMap;
private JMenuItem saveMenuItem;
private ImageEditorPanel imageEditor;
public MainFrame() throws HeadlessException {
super("Draw 9-patch");
buildActions();
buildMenuBar();
buildContent();
showOpenFilePanel();
// pack();
setSize(1024, 600);
}
private void buildActions() {
actionsMap = new ActionMap();
actionsMap.put(OpenAction.ACTION_NAME, new OpenAction(this));
actionsMap.put(SaveAction.ACTION_NAME, new SaveAction(this));
actionsMap.put(ExitAction.ACTION_NAME, new ExitAction(this));
}
private void buildMenuBar() {
JMenu fileMenu = new JMenu("File");
JMenuItem openMenuItem = new JMenuItem();
saveMenuItem = new JMenuItem();
JMenuItem exitMenuItem = new JMenuItem();
openMenuItem.setAction(actionsMap.get(OpenAction.ACTION_NAME));
fileMenu.add(openMenuItem);
saveMenuItem.setAction(actionsMap.get(SaveAction.ACTION_NAME));
saveMenuItem.setEnabled(false);
fileMenu.add(saveMenuItem);
exitMenuItem.setAction(actionsMap.get(ExitAction.ACTION_NAME));
fileMenu.add(exitMenuItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
setJMenuBar(menuBar);
}
private void buildContent() {
setContentPane(new GradientPanel());
}
private void showOpenFilePanel() {
add(new OpenFilePanel(this));
}
public SwingWorker<?, ?> open(File file) {
if (file == null) {
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new PngFileFilter());
int choice = chooser.showOpenDialog(this);
if (choice == JFileChooser.APPROVE_OPTION) {
return new OpenTask(chooser.getSelectedFile());
} else {
return null;
}
} else {
return new OpenTask(file);
}
}
void showImageEditor(BufferedImage image, String name) {
getContentPane().removeAll();
imageEditor = new ImageEditorPanel(this, image, name);
add(imageEditor);
saveMenuItem.setEnabled(true);
validate();
repaint();
}
public SwingWorker<?, ?> save() {
if (imageEditor == null) {
return null;
}
File file = imageEditor.chooseSaveFile();
return file != null ? new SaveTask(file) : null;
}
private class SaveTask extends SwingWorker<Boolean, Void> {
private final File file;
SaveTask(File file) {
this.file = file;
}
protected Boolean doInBackground() throws Exception {
try {
ImageIO.write(imageEditor.getImage(), "PNG", file);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}
private class OpenTask extends SwingWorker<BufferedImage, Void> {
private final File file;
OpenTask(File file) {
this.file = file;
}
protected BufferedImage doInBackground() throws Exception {
return GraphicsUtilities.loadCompatibleImage(file.toURI().toURL());
}
@Override
protected void done() {
try {
showImageEditor(get(), file.getAbsolutePath());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}

View File

@@ -0,0 +1,51 @@
package com.android.draw9patch.ui;
import com.android.draw9patch.graphics.GraphicsUtilities;
import javax.swing.JComponent;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.io.IOException;
import java.net.URL;/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class OpenFilePanel extends JComponent {
private BufferedImage dropHere;
OpenFilePanel(MainFrame mainFrame) {
setOpaque(false);
loadSupportImage();
setTransferHandler(new ImageTransferHandler(mainFrame));
}
private void loadSupportImage() {
try {
URL resource = getClass().getResource("/images/drop.png");
dropHere = GraphicsUtilities.loadCompatibleImage(resource);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
int x = (getWidth() - dropHere.getWidth()) / 2;
int y = (getHeight() - dropHere.getHeight()) / 2;
g.drawImage(dropHere, x, y, null);
}
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.draw9patch.ui;
import javax.swing.filechooser.FileFilter;
import java.io.File;
class PngFileFilter extends FileFilter {
@Override
public boolean accept(File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith(".png");
}
@Override
public String getDescription() {
return "PNG Image (*.png)";
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.draw9patch.ui.action;
import org.jdesktop.swingworker.SwingWorker;
import javax.swing.AbstractAction;
public abstract class BackgroundAction extends AbstractAction {
protected void executeBackgroundTask(SwingWorker<?, ?> worker) {
if (worker != null) {
worker.execute();
}
}
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.draw9patch.ui.action;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
import javax.swing.JFrame;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
public class ExitAction extends AbstractAction {
public static final String ACTION_NAME = "exit";
private JFrame frame;
public ExitAction(JFrame frame) {
putValue(NAME, "Quit");
putValue(SHORT_DESCRIPTION, "Quit");
putValue(LONG_DESCRIPTION, "Quit");
putValue(MNEMONIC_KEY, KeyEvent.VK_Q);
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_Q,
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
this.frame = frame;
}
public void actionPerformed(ActionEvent e) {
frame.dispose();
System.exit(0);
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.draw9patch.ui.action;
import com.android.draw9patch.ui.MainFrame;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
public class OpenAction extends BackgroundAction {
public static final String ACTION_NAME = "open";
private MainFrame frame;
public OpenAction(MainFrame frame) {
this.frame = frame;
putValue(NAME, "Open 9-patch...");
putValue(SHORT_DESCRIPTION, "Open...");
putValue(LONG_DESCRIPTION, "Open 9-patch...");
putValue(MNEMONIC_KEY, KeyEvent.VK_O);
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O,
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
}
public void actionPerformed(ActionEvent e) {
executeBackgroundTask(frame.open(null));
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.draw9patch.ui.action;
import com.android.draw9patch.ui.MainFrame;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
public class SaveAction extends BackgroundAction {
public static final String ACTION_NAME = "save";
private MainFrame frame;
public SaveAction(MainFrame frame) {
this.frame = frame;
putValue(NAME, "Save 9-patch...");
putValue(SHORT_DESCRIPTION, "Save...");
putValue(LONG_DESCRIPTION, "Save 9-patch...");
putValue(MNEMONIC_KEY, KeyEvent.VK_S);
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S,
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
}
public void actionPerformed(ActionEvent e) {
executeBackgroundTask(frame.save());
}
}