/** * Copyright (c) 2012-2013 Nokia Corporation. All rights reserved. * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. * Oracle and Java are trademarks or registered trademarks of Oracle and/or its * affiliates. Other product and company names mentioned herein may be trademarks * or trade names of their respective owners. * See LICENSE.TXT for license information. */ package com.nokia.example.paint.views.components; import com.nokia.example.paint.Main; import com.nokia.example.paint.helpers.FileHandler; import com.nokia.example.paint.helpers.ImageLoader; import com.nokia.example.paint.helpers.Rectangle; import com.nokia.example.paint.views.DrawArea; import com.nokia.mid.ui.TextEditor; import com.nokia.mid.ui.TextEditorListener; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Font; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; import javax.microedition.lcdui.TextField; /* * Implements the file saving dialog. */ public class FileSaveDialog implements TextEditorListener, ButtonListener { private TextEditor texteditor; private boolean isShown = false; protected OKButton okbutton; protected CancelButton cancelbutton; protected CharEraserButton chareraserbutton; private Rectangle dialogArea; private Image background; private int x = 0; private int y = 160; private boolean showEraser = false; private boolean quitAfterSave = false; public FileSaveDialog(Canvas parent) { ImageLoader loader = ImageLoader.getInstance(); background = loader.loadImage("/dialog.png"); try { // If there is a virtual keyboard, put save dialog to top. Class.forName("com.nokia.mid.ui.VirtualKeyboard"); y = 0; } catch (ClassNotFoundException e) { } int width = parent.getWidth(); if (width > 240) { x = (width - background.getWidth()) / 2; } Font font = Font.getDefaultFont(); texteditor = TextEditor.createTextEditor("", 50, TextField.ANY, 200, font.getHeight()); texteditor.setParent(parent); texteditor.setPosition(x + 20, y + 60); texteditor.setBackgroundColor(0xFFFFFFFF); texteditor.setForegroundColor(0xFF000000); texteditor.setConstraints(TextField.NON_PREDICTIVE); texteditor.setTextEditorListener(this); dialogArea = new Rectangle(x, y, background.getWidth(), background.getHeight()); okbutton = new OKButton(); okbutton.addListener(this); cancelbutton = new CancelButton(); cancelbutton.addListener(this); okbutton.setPosition(x, y + 122); cancelbutton.setPosition(x + 120, y + 122); chareraserbutton = new CharEraserButton(texteditor); chareraserbutton.setPosition(x + 120, y + 122); } public void render(Graphics g) { if (isShown) { g.drawImage(background, x, y, Graphics.LEFT | Graphics.TOP); okbutton.render(g); if (!showEraser) { cancelbutton.render(g); } else { chareraserbutton.render(g); } g.setColor(0x000000); g.drawString("Give filename:", x + 20, y + 35, Graphics.LEFT | Graphics.BOTTOM); } } public void hide() { isShown = false; texteditor.setVisible(false); } public void show(boolean quitAfterSave) { this.quitAfterSave = quitAfterSave; isShown = true; texteditor.setVisible(true); texteditor.setFocus(true); } public boolean isVisible() { return isShown; } public void pointerPressed(int x, int y) { okbutton.pressed(x, y); if (showEraser) { chareraserbutton.pressed(x, y); } else { cancelbutton.pressed(x, y); } } public void pointerUnpressed(int x, int y) { if (!dialogArea.isPointInside(x, y)) { isShown = false; texteditor.setVisible(false); Main.getInstance().getDrawArea().allowDrawing(true); } else { okbutton.unpressed(x, y); if (showEraser) { chareraserbutton.unpressed(x, y); } else { cancelbutton.unpressed(x, y); } } } public void inputAction(TextEditor te, int actions) { if (texteditor.getContent().length() > 0) { showEraser = true; } else { showEraser = false; } } public void buttonPressed(Button caller) { if (caller.equals(okbutton)) { saveToFile(); } //both buttons will hide the dialog isShown = false; texteditor.setVisible(false); Main.getInstance().getDrawArea().allowDrawing(true); } private void saveToFile() { DrawArea drawArea = Main.getInstance().getDrawArea(); final Image saveable = drawArea.getDrawing(); FileHandler.saveImageToFile(texteditor.getContent(), saveable, quitAfterSave); drawArea.setDrawingChanged(false); } }