/** * Copyright 1999-2009 The Pegadi Team * * 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 org.pegadi.storysketch.views; import org.jgraph.JGraph; import org.jgraph.graph.*; import org.pegadi.storysketch.cells.Quote; import org.pegadi.storysketch.cells.QuoteCell; import javax.swing.*; import javax.swing.border.Border; import java.awt.*; import java.util.EventObject; public class QuoteView extends VertexView { static QuoteRenderer renderer = new QuoteRenderer(); static QuoteEditor editor = null; // Constructor for Superclass public QuoteView(Object cell, JGraph graph,CellMapper cm) { super(cell, graph, cm); } /* // Returns Perimeter Point for Quote public Point getPerimeterPoint(Point source, Point p) { ... } */ // Returns the Renderer for this View public CellViewRenderer getRenderer() { return renderer; } public GraphCellEditor getEditor() { if(editor == null) editor = new QuoteEditor(); return editor; } } // Define the Renderer for an QuoteView class QuoteRenderer extends VertexRenderer { JPanel component = new JPanel(); JTextArea textArea = new JTextArea(); public QuoteRenderer() { component.setOpaque(false); component.setBorder(new QuoteBorder()); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); textArea.setFont(new Font("SansSerif", Font.PLAIN,9)); //textArea.setBackground(Color.decode("#ffffaa")); textArea.setBackground(new Color(250,243,224)); component.setBackground(textArea.getBackground()); component.setLayout(new BorderLayout()); component.add(textArea, BorderLayout.CENTER); } class QuoteBorder implements Border { int CORNER = 20; int TH = CORNER/2; public QuoteBorder() { } public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { g.setColor(c.getBackground()); g.fillRoundRect(x,y,width-1,height-1-TH, CORNER, CORNER); g.setColor(Color.BLACK); g.drawRoundRect(x,y,width-1,height-1-TH, CORNER, CORNER); Polygon p = new Polygon(); p.addPoint(x+CORNER/2,y+height-1-TH); p.addPoint(x+CORNER/4, y+height-1); p.addPoint(x+CORNER, y+height-1-TH); g.setColor(c.getBackground()); g.fillPolygon(p); g.setColor(Color.BLACK); g.drawPolygon(p); g.setColor(c.getBackground()); g.drawLine(x+CORNER/2,y+height-1-TH, x+CORNER-1, y+height-1-TH); } public Insets getBorderInsets(Component c) { return new Insets(CORNER/4, 3, (int)(CORNER*0.75), 3); } public boolean isBorderOpaque() { return false; } } public Component getRendererComponent(JGraph graph, CellView view, boolean sel, boolean focus, boolean preview) { QuoteCell nc = (QuoteCell) view.getCell(); Quote quote = (Quote) nc.getUserObject(); textArea.setText(quote.getQuote()); return component; } } class QuoteEditor extends StorySketchCellEditor { public QuoteEditor() { super(); } class RealCellEditor extends AbstractCellEditor implements GraphCellEditor { JPanel editorComponent = new JPanel(); JTextArea textArea = new JTextArea(15, 25); public RealCellEditor() { editorComponent.setBorder(UIManager.getBorder("Tree.editorBorder")); textArea.setSize(200,150); //textArea.setBackground(Color.decode("#ffffaa")); textArea.setBackground(new Color(250,243,224)); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); editorComponent.setLayout(new BorderLayout()); JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); editorComponent.add(scroll,BorderLayout.CENTER); } public Component getGraphCellEditorComponent( JGraph graph, Object value, boolean isSelected) { QuoteCell nc = (QuoteCell) editingCell; Quote q = (Quote) nc.getUserObject(); textArea.setText(q.getQuote()); return editorComponent; } public Object getCellEditorValue() { return textArea.getText(); } public boolean stopCellEditing() { QuoteCell nc = (QuoteCell) graph.getEditingCell(); Quote n = (Quote) nc.getUserObject(); n.setQuote(textArea.getText()); return true; } public boolean shouldSelectCell(EventObject event) { textArea.requestFocus(); return super.shouldSelectCell(event); } } protected GraphCellEditor createGraphCellEditor() { return new QuoteEditor.RealCellEditor(); } }