/**
* 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.Note;
import org.pegadi.storysketch.cells.NoteCell;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.util.EventObject;
public class NoteView extends VertexView {
static NoteRenderer renderer = new NoteRenderer();
static NoteEditor editor = null;
// Constructor for Superclass
public NoteView(Object cell, JGraph graph,CellMapper cm) {
super(cell, graph, cm);
}
/*
// Returns Perimeter Point for Notes
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 NoteEditor();
return editor;
}
}
// Define the Renderer for an NoteView
class NoteRenderer extends VertexRenderer {
JPanel component = new JPanel();
JTextArea textArea = new JTextArea();
public NoteRenderer() {
component.setOpaque(false);
component.setBorder(new NoteBorder());
textArea.setLineWrap(true);
textArea.setWrapStyleWord(false);
textArea.setFont(new Font("SansSerif", Font.PLAIN,9));
textArea.setBackground(Color.decode("#ffffaa"));
component.setBackground(textArea.getBackground());
component.setLayout(new BorderLayout());
component.add(textArea, BorderLayout.CENTER);
}
class NoteBorder implements Border {
int corner;
public NoteBorder() {
corner = 10;
}
public void paintBorder(Component c,
Graphics g,
int x,
int y,
int width,
int height) {
Polygon p = new Polygon();
p.addPoint(x,y);
p.addPoint(x, y+height-1);
p.addPoint(x + width-1, y+height-1);
p.addPoint(x + width-1,corner);
p.addPoint(x + width-1 - corner,y);
g.setColor(c.getBackground());
g.fillPolygon(p);
g.setColor(Color.BLACK);
g.drawPolygon(p);
// Draw the flip
g.drawLine(x+width-1-corner,
y,
x+width-1-corner,
y+corner-1);
g.drawLine(x+width-1-corner,
y+corner-1,
x+width-1,
y+corner-1);
}
public Insets getBorderInsets(Component c) {
return new Insets(corner, 2, 2, 2);
}
public boolean isBorderOpaque() {
return false;
}
}
public Component getRendererComponent(JGraph graph,
CellView view,
boolean sel,
boolean focus,
boolean preview) {
NoteCell nc = (NoteCell) view.getCell();
Note note = (Note) nc.getUserObject();
textArea.setText(note.getNotes());
return component;
}
}
class NoteEditor extends StorySketchCellEditor {
public NoteEditor() {
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.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) {
NoteCell nc = (NoteCell) editingCell;
Note n = (Note) nc.getUserObject();
textArea.setText(n.getNotes());
return editorComponent;
}
public Object getCellEditorValue() {
return textArea.getText();
}
public boolean stopCellEditing() {
NoteCell nc = (NoteCell) graph.getEditingCell();
Note n = (Note) nc.getUserObject();
n.setNotes(textArea.getText());
return true;
}
public boolean shouldSelectCell(EventObject event) {
textArea.requestFocus();
return super.shouldSelectCell(event);
}
}
protected GraphCellEditor createGraphCellEditor() {
return new NoteEditor.RealCellEditor();
}
}