/*
* Copyright 2013 Serdar.
*
* 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 de.fub.agg2graphui;
import de.fub.agg2graphui.controller.AbstractLayer;
import de.fub.agg2graphui.layers.LayerEvent;
import de.fub.agg2graphui.layers.LayerListener;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
/**
*
* @author Serdar
*/
public class LayerPanel extends javax.swing.JPanel implements LayerListener {
private static final long serialVersionUID = 1L;
private transient AbstractLayer<?> layer;
/**
* Creates new form LayerPanel
*/
public LayerPanel() {
initComponents();
super.setOpaque(false);
}
public LayerPanel(AbstractLayer<?> layer) {
this();
setLayer(layer);
}
public final void setLayer(AbstractLayer<?> layer) {
this.layer = layer;
layer.addLayerListener(LayerPanel.this);
}
@Override
public void setOpaque(boolean isOpaque) {
super.setOpaque(false);
}
public BufferedImage getImage() {
return (BufferedImage) (layer != null
? layer.getImage()
: ((getWidth() > 0 && getHeight() > 0)
? createImage(getWidth(), getHeight()) : null));
}
public String getDescription() {
return layer != null ? layer.getDescription() : null;
}
@Override
public String getName() {
return layer != null ? layer.getName() : super.getName();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
setPreferredSize(new java.awt.Dimension(0, 258));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 302, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 119, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
@Override
protected void paintComponent(Graphics g) {
synchronized (getTreeLock()) {
super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
if (layer != null) {
Graphics2D g2d = (Graphics2D) g;
Dimension size = layer.getLayerManager().getSize();
double scaleFactor = getWidth() / size.getWidth();
g2d.scale(scaleFactor, scaleFactor);
layer.paintLayer(g, new Rectangle(getSize()));
}
}
}
@Override
public void requestRender(LayerEvent event) {
if (layer == event.getSource()) {
repaint();
}
}
}