📄 cell.java
字号:
((JPanel)editor).setBackground(Color.WHITE);
editor.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent arg0) {
if (isReadOnly()) return;
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new ReportImageFileFilter());
if(chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
loadImage(chooser.getSelectedFile().getAbsolutePath());
editor.updateUI();
};
}});
}
if (editor != null) {
editor.setBorder(null);
editor.setFont(font);
editor.setBackground(Color.WHITE);
editor.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent arg0) {
if (report != null && report.hasCellEnterListener())
report.fireCellEnter(instance);
report.currentPage.currentCell = instance;
}
public void focusLost(FocusEvent arg0) {
if (!getValue().equals(instance.getEditorValue())) {
setValue(instance.getEditorValue());
}
if (report != null && report.hasCellLeaveListener())
report.fireCellLeave(instance);
}
});
setAlignment(alignment);
setEditorValue(value);
}
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
if (value == null) value = "";
if (!value.equals(this.value)) {
if (report != null && report.hasValueChangedListener())
report.fireValueChanged(this, value, this.value);
}
Object editorValue = getEditorValue();
if (editorValue == null || !editorValue.equals(value)) setEditorValue(value);
this.value = value;
}
public boolean isEmpty(){
if ((getName()==null || getName().equals(""))
&& (getValue()==null || getValue().equals(""))) return true;
else return false;
}
public Font getFont() {
return font;
}
public void setFont(Font font) {
this.font = font;
if (editor != null) {
editor.setFont(font);
editor.updateUI();
}
if (editor != null) {
editor.setBorder(null);
}
}
public int getAlignment() {
return alignment;
}
public void setAlignment(int alignment) {
this.alignment = alignment;
if (editor != null) {
if (editor instanceof JLabel) {
if (alignment == ALIGN_LEFT)
((JLabel)editor).setHorizontalAlignment(JLabel.LEFT);
else if (alignment == ALIGN_CENTER)
((JLabel)editor).setHorizontalAlignment(JLabel.CENTER);
else if (alignment == ALIGN_RIGHT)
((JLabel)editor).setHorizontalAlignment(JLabel.RIGHT);
} else if (editor instanceof JTextField) {
if (alignment == ALIGN_LEFT)
((JTextField)editor).setHorizontalAlignment(JTextField.LEFT);
else if (alignment == ALIGN_CENTER)
((JTextField)editor).setHorizontalAlignment(JTextField.CENTER);
else if (alignment == ALIGN_RIGHT)
((JTextField)editor).setHorizontalAlignment(JTextField.RIGHT);
} else if (editor instanceof JTextArea) {
}
}
}
public static String getTypeDescription(int type) {
return StringResource.getString("cellType" + String.valueOf(type));
}
public String[] getComboxOptions() {
return comboxOptions;
}
public void setComboxOptions(String[] comboxOptions) {
this.comboxOptions = comboxOptions;
}
public String getComboxOptionsStr() {
String result = "";
if (comboxOptions != null) {
for (int i = 0; i < comboxOptions.length; i++) {
if (i == 0) result = comboxOptions[i];
else result = result + "\n" + comboxOptions[i];
}
}
return result;
}
public void setComboxOptionsStr(String comboxOptionsStr) {
if (comboxOptionsStr == null || comboxOptionsStr.equals("")) return;
setComboxOptions(comboxOptionsStr.split("\n"));
}
public void registerEditor() {
if (editor == null) {
createEditor();
}
if (editor != null) {
try {
report.add(editor);
setEditorBounds();
} catch(Exception e) {
e.printStackTrace();
}
}
}
public void printRender(Graphics2D arg0) {
if (type == TYPE_TEXTAREA) {
arg0.translate(x, y);
editor.paint(arg0);
arg0.translate(-x, -y);
return;
}
drawContent(arg0);
}
public int getContentHeight(Graphics g){
String value = getStrValue();
if (value == null || value.equals("")) return 0;
String[] strs = value.split("\n");
if (strs == null || strs.length == 0) return 0;
return ((int) g.getFontMetrics(font).getStringBounds(" ", g).getHeight()
* (strs.length) + innerMargin * 2);
}
public String getStrValue() {
// try {
// System.out.println((String)value);
// } catch(Exception e) {
// System.out.println(this.getTypeDescription(type));
// System.out.println(value.getClass());
// }
if (value instanceof Date) {
if (value != null)
return FJDateField.defaultDf.format(value);
else return "";
}
return (String) value;
}
public FJReport getReport() {
return report;
}
public void setReport(FJReport report) {
this.report = report;
}
public Object getEditorValue(){
if (editor == null) return value;
if (type == TYPE_LABEL) {
return ((JLabel)editor).getText();
} else if (type == this.TYPE_TEXTFIELD) {
return ((JTextField)editor).getText();
} else if (type == this.TYPE_TEXTAREA) {
return ((JTextArea)editor).getText();
} else if (type == this.TYPE_COMBOBOX) {
return (String) ((JComboBox)editor).getSelectedItem();
} else if (type == this.TYPE_CHECKBOX) {
return String.valueOf(((JCheckBox)editor).isSelected());
} else if (type == this.TYPE_DATEFIELD) {
return ((FJDateField)editor).getValue();
}
return value;
}
public void setEditorValue(Object value) {
if (editor == null) return;
if (type == TYPE_LABEL) {
((JLabel)editor).setText((String) value);
} else if (type == TYPE_TEXTFIELD) {
((JTextField)editor).setText((String) value);
} else if (type == TYPE_TEXTAREA) {
((JTextArea)editor).setText((String) value);
} else if (type == TYPE_COMBOBOX) {
((JComboBox)editor).setSelectedItem(value);
} else if (type == TYPE_CHECKBOX) {
if (value == null || "".equals(value)) ((JCheckBox)editor).setSelected(false);
else ((JCheckBox)editor).setSelected((Boolean) value);
} else if (type == TYPE_DATEFIELD) {
if (value instanceof Date)
((FJDateField)editor).setValue((Date) value);
else if (value instanceof String)
((FJDateField)editor).setDateStr((String) value);
} else if (type == TYPE_IMAGE) {
if (value instanceof String) {
if (value.equals("")) imgValue = null;
else loadImage((String) value);
} else if (value instanceof Image) {
createBufferredImage((Image) value);
} else imgValue = null;
editor.updateUI();
}
}
private void loadImage(String imgFileName) {
Image img = null;
URL url = null;
try {
url = new URL(imgFileName);
} catch (MalformedURLException e) {
url = null;
}
if (url != null)
img = Toolkit.getDefaultToolkit().getImage(url);
else img = Toolkit.getDefaultToolkit().getImage(imgFileName);
MediaTracker imgLoadCheck = new MediaTracker(report);
imgLoadCheck.addImage(img, 0);
try {
imgLoadCheck.waitForID(0);
createBufferredImage(img);
} catch (Exception e) {
e.printStackTrace();
}
}
private void createBufferredImage(Image img) {
int srcWidth, srcHeight, dstWidth, dstHeight;
srcWidth = img.getWidth(null);
srcHeight = img.getHeight(null);
double zoom = Math.min(((double)width-margin.left-margin.right)/srcWidth, (double)(height-margin.top-margin.bottom)/srcHeight);
dstWidth = (int)((double)srcWidth * zoom);
dstHeight = (int)((double)srcHeight * zoom);
imgValue = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_RGB);
imgValue.getGraphics().drawImage(img, 0, 0, dstWidth, dstHeight, 0, 0, srcWidth, srcHeight, null);
}
public boolean isReadOnly() {
return readOnly ||
(report != null && report.getState() != report.EDIT_STATE);
}
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
if (editor != null) {
if (editor instanceof JTextField) {
((JTextField)editor).setEditable(!readOnly);;
} else if (editor instanceof JTextArea) {
((JTextArea)editor).setEditable(!readOnly);
} else {
editor.setEnabled(!readOnly);
}
}
}
public Insets getMargin() {
return margin;
}
public void setMargin(Insets margin) {
this.margin = margin;
}
public void setEditorBounds() {
if (editor != null)
editor.setBounds(x + margin.left, y + margin.top, width - margin.left - margin.right, height - margin.top - margin.bottom);
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -