You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
316 lines
13 KiB
316 lines
13 KiB
package com.fr.design.sort.common; |
|
|
|
import com.fr.base.Style; |
|
import com.fr.base.background.ColorBackground; |
|
import com.fr.base.svg.IconUtils; |
|
import com.fr.design.event.UIObserver; |
|
import com.fr.design.event.UIObserverListener; |
|
import com.fr.design.file.HistoryTemplateListCache; |
|
import com.fr.design.gui.ibutton.UIButton; |
|
import com.fr.design.gui.ilable.UILabel; |
|
import com.fr.design.mainframe.ElementCasePane; |
|
import com.fr.design.mainframe.JTemplate; |
|
import com.fr.design.mainframe.JTemplateActionListener; |
|
import com.fr.design.selection.SelectionEvent; |
|
import com.fr.design.selection.SelectionListener; |
|
import com.fr.design.sort.header.HeaderAreaPane; |
|
import com.fr.grid.selection.CellSelection; |
|
import com.fr.grid.selection.Selection; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.report.cell.DefaultTemplateCellElement; |
|
import com.fr.report.cell.TemplateCellElement; |
|
import com.fr.report.elementcase.TemplateElementCase; |
|
import com.fr.stable.ColumnRow; |
|
import com.fr.stable.EssentialUtils; |
|
import com.fr.stable.StringUtils; |
|
|
|
import javax.swing.Icon; |
|
import javax.swing.JPanel; |
|
import javax.swing.JTextField; |
|
import java.awt.Color; |
|
import java.awt.Dimension; |
|
import java.awt.FlowLayout; |
|
import java.awt.event.MouseAdapter; |
|
import java.awt.event.MouseEvent; |
|
import java.util.ArrayList; |
|
import java.util.HashMap; |
|
import java.util.Map; |
|
|
|
public class SortColumnRowPane extends JPanel implements UIObserver { |
|
int paneWidth; |
|
int paneHeight; |
|
int jTextFieldWidth; |
|
JTextField colJTextField; |
|
JTextField rowJTextField; |
|
UIButton selectButton; |
|
private boolean isAlreadyAddListener = false; |
|
private CellSelection oldSelection; |
|
private SelectionListener gridSelectionChangeListener; |
|
UIObserverListener uiObserverListener; |
|
private final static Icon DISABLED_ICON = IconUtils.readIcon("/com/fr/design/images/buttonicon/select_disabled.svg"); |
|
private final static Icon ENABLE_ICON = IconUtils.readIcon("/com/fr/design/images/buttonicon/select_normal.svg"); |
|
private boolean enabled; |
|
SelectActionListener selectActionListener; |
|
|
|
HeaderAreaPane.CellSelectionManager cellSelectionManager; |
|
|
|
public SortColumnRowPane(int paneWidth, int paneHeight) { |
|
this.paneWidth = paneWidth; |
|
this.paneHeight = paneHeight; |
|
initComponents(); |
|
} |
|
|
|
private void initComponents() { |
|
initSize(); |
|
this.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); |
|
intUILabel(); |
|
initTextField(); |
|
initSelectButton(); |
|
this.setSize(new Dimension(paneWidth, paneHeight)); |
|
} |
|
|
|
void initSize() { |
|
jTextFieldWidth = (paneWidth - 40) / 2; |
|
} |
|
|
|
void intUILabel() { |
|
UILabel uiLabel = new UILabel(IconUtils.readIcon("/com/fr/design/images/buttonicon/propertiestab/cellelement_normal.png")); |
|
this.add(uiLabel); |
|
} |
|
|
|
public static boolean isAvailableColumnRow(ColumnRow columnRow) { |
|
return columnRow != null && columnRow.getRow() != -1 && columnRow.getColumn() != -1; |
|
} |
|
|
|
void initTextField() { |
|
colJTextField = new JTextField(); |
|
colJTextField.setEditable(false); |
|
rowJTextField = new JTextField(); |
|
rowJTextField.setEditable(false); |
|
colJTextField.setPreferredSize(new Dimension(jTextFieldWidth, paneHeight)); |
|
rowJTextField.setPreferredSize(new Dimension(jTextFieldWidth, paneHeight)); |
|
this.add(colJTextField); |
|
this.add(rowJTextField); |
|
} |
|
|
|
void initSelectButton() { |
|
selectButton = new UIButton(ENABLE_ICON); |
|
selectActionListener = new SelectActionListener(this); |
|
selectButton.addMouseListener(selectActionListener); |
|
this.add(selectButton); |
|
} |
|
|
|
public void populateBean(ColumnRow columnRow, boolean enabled, HeaderAreaPane.CellSelectionManager cellSelectionManager) { |
|
this.cellSelectionManager = cellSelectionManager; |
|
populateBean(columnRow, enabled); |
|
} |
|
|
|
public void populateBean(ColumnRow columnRow) { |
|
populateBean(columnRow, true); |
|
} |
|
|
|
public void populateBean(ColumnRow columnRow, boolean enabled) { |
|
this.enabled = enabled; |
|
if (SortColumnRowPane.isAvailableColumnRow(columnRow)) { |
|
colJTextField.setText(EssentialUtils.convertIntToABC(columnRow.column + 1)); |
|
rowJTextField.setText(String.valueOf(columnRow.row + 1)); |
|
} else { |
|
colJTextField.setText(StringUtils.EMPTY); |
|
rowJTextField.setText(StringUtils.EMPTY); |
|
} |
|
if (enabled) { |
|
selectButton.setIcon(ENABLE_ICON); |
|
} else { |
|
selectButton.setIcon(DISABLED_ICON); |
|
} |
|
selectButton.setEnabled(false); |
|
refresh(); |
|
} |
|
|
|
public void setColumnRow(ColumnRow columnRow) { |
|
populateBean(columnRow); |
|
uiObserverListener.doChange(); |
|
} |
|
|
|
public ColumnRow updateBean() { |
|
if (StringUtils.isNotBlank(colJTextField.getText()) && StringUtils.isNotBlank(rowJTextField.getText())) { |
|
return ColumnRow.valueOf(colJTextField.getText() + rowJTextField.getText()); |
|
} |
|
return ColumnRow.ERROR; |
|
} |
|
|
|
@Override |
|
public void registerChangeListener(UIObserverListener listener) { |
|
this.uiObserverListener = listener; |
|
} |
|
|
|
@Override |
|
public boolean shouldResponseChangeListener() { |
|
return true; |
|
} |
|
|
|
|
|
public void cancelSelectState() { |
|
selectActionListener.recoverSelectHeader(SortUtils.getCurrentElementCase()); |
|
} |
|
|
|
class SelectActionListener extends MouseAdapter { |
|
SortColumnRowPane columnRowPane; |
|
ColumnRow oldColumnRow; |
|
JTemplateActionListener jTemplateActionListener; |
|
|
|
Map<ColumnRow, Style> disableHeaderCellsStyleMap = new HashMap<>(); |
|
java.util.List<TemplateCellElement> tempHeaderCells = new ArrayList<>(); |
|
|
|
SelectActionListener(SortColumnRowPane columnRowPane) { |
|
this.columnRowPane = columnRowPane; |
|
} |
|
|
|
@Override |
|
public void mouseClicked(MouseEvent e) { |
|
if (enabled) { |
|
ElementCasePane elementCasePane = SortUtils.getCurrentElementCase(); |
|
if (elementCasePane == null || isAlreadyAddListener) { |
|
return; |
|
} |
|
oldColumnRow = columnRowPane.updateBean(); |
|
prepareSelectHeader(elementCasePane); |
|
gridSelectionChangeListener = new SelectionListener() { |
|
@Override |
|
public void selectionChanged(SelectionEvent e) { |
|
completeSelectHeader(elementCasePane); |
|
removeJTemplateActionListener(); |
|
} |
|
}; |
|
elementCasePane.addSelectionChangeListener(gridSelectionChangeListener); |
|
this.addJTemplateActionListener(); |
|
isAlreadyAddListener = true; |
|
} |
|
} |
|
|
|
|
|
private void addJTemplateActionListener() { |
|
JTemplate jTemplate = HistoryTemplateListCache.getInstance().getCurrentEditingTemplate(); |
|
if (jTemplate != null) { |
|
removeJTemplateActionListener(); |
|
jTemplateActionListener = new JTemplateActionListener() { |
|
@Override |
|
public void templateSaveBefore(JTemplate<?, ?> jt) { |
|
ElementCasePane elementCasePane = SortUtils.getCurrentElementCase(); |
|
if (elementCasePane != null) { |
|
recoverSelectHeader(elementCasePane); |
|
} |
|
removeJTemplateActionListener(); |
|
} |
|
}; |
|
jTemplate.addJTemplateActionListener(jTemplateActionListener); |
|
} |
|
} |
|
|
|
private void removeJTemplateActionListener() { |
|
JTemplate jTemplate = HistoryTemplateListCache.getInstance().getCurrentEditingTemplate(); |
|
if (jTemplate != null) { |
|
if (jTemplateActionListener != null) { |
|
jTemplate.removeJTemplateActionListener(jTemplateActionListener); |
|
} |
|
} |
|
} |
|
|
|
|
|
private void prepareSelectHeader(ElementCasePane elementCasePane) { |
|
ashDisableHeaderCellsStyle(elementCasePane.getEditingElementCase()); |
|
oldSelection = (CellSelection) elementCasePane.getSelection(); |
|
elementCasePane.getGrid().setNotShowingTableSelectPane(false); |
|
elementCasePane.setRepeatSelection(true); |
|
elementCasePane.setEditable(false); |
|
elementCasePane.repaint(10); |
|
} |
|
|
|
private void completeSelectHeader(ElementCasePane elementCasePane) { |
|
Selection selection = elementCasePane.getSelection(); |
|
if (selection instanceof CellSelection) { |
|
CellSelection cellselection = (CellSelection) selection; |
|
ColumnRow columnRow = ColumnRow.valueOf(cellselection.getColumn(), cellselection.getRow()); |
|
elementCasePane.setOldSelecton(oldSelection); |
|
oldSelection.getQuickEditor(elementCasePane); |
|
if (cellSelectionManager == null || !cellSelectionManager.isNotSelectables(columnRow)) { |
|
if (cellSelectionManager != null) { |
|
cellSelectionManager.removeNotSelectables(oldColumnRow); |
|
cellSelectionManager.addNotSelectables(columnRow); |
|
} |
|
columnRowPane.setColumnRow(columnRow); |
|
} |
|
} |
|
recoverSelectHeader(elementCasePane); |
|
} |
|
|
|
private void recoverSelectHeader(ElementCasePane elementCasePane) { |
|
if (isAlreadyAddListener) { |
|
restoreDisableHeaderCellsStyle(elementCasePane.getEditingElementCase()); |
|
elementCasePane.removeSelectionChangeListener(gridSelectionChangeListener); |
|
isAlreadyAddListener = false; |
|
elementCasePane.getGrid().setNotShowingTableSelectPane(true); |
|
elementCasePane.setRepeatSelection(false); |
|
elementCasePane.setEditable(true); |
|
elementCasePane.repaint(); |
|
oldColumnRow = null; |
|
} |
|
} |
|
|
|
private void ashDisableHeaderCellsStyle(TemplateElementCase elementCase) { |
|
if (cellSelectionManager != null) { |
|
java.util.List<ColumnRow> notSelectables = cellSelectionManager.getNotSelectables(); |
|
disableHeaderCellsStyleMap = new HashMap<>(); |
|
tempHeaderCells = new ArrayList<>(); |
|
for (ColumnRow columnRow : notSelectables) { |
|
TemplateCellElement templateCellElement = null; |
|
if (columnRow != ColumnRow.ERROR) { |
|
templateCellElement = elementCase.getTemplateCellElement(columnRow.column, columnRow.row); |
|
} |
|
if (templateCellElement == null) { |
|
templateCellElement = new DefaultTemplateCellElement(columnRow.column, columnRow.row); |
|
elementCase.addCellElement(templateCellElement); |
|
tempHeaderCells.add(templateCellElement); |
|
} |
|
Style style = templateCellElement.getStyle(); |
|
disableHeaderCellsStyleMap.put(columnRow, style); |
|
style = style.deriveBackground(ColorBackground.getInstance(Color.gray)); |
|
templateCellElement.setStyle(style); |
|
} |
|
} |
|
|
|
} |
|
|
|
private void restoreDisableHeaderCellsStyle(TemplateElementCase elementCase) { |
|
if (cellSelectionManager != null) { |
|
try { |
|
for (ColumnRow headerColumnRow : disableHeaderCellsStyleMap.keySet()) { |
|
TemplateCellElement headerTemplateCellElement = null; |
|
if (headerColumnRow != ColumnRow.ERROR) { |
|
headerTemplateCellElement |
|
= elementCase.getTemplateCellElement(headerColumnRow.column, headerColumnRow.row); |
|
} |
|
if (headerTemplateCellElement != null) { |
|
headerTemplateCellElement.setStyle(disableHeaderCellsStyleMap.get(headerColumnRow)); |
|
} |
|
} |
|
for (TemplateCellElement templateCellElement : tempHeaderCells) { |
|
elementCase.removeCellElement(templateCellElement); |
|
} |
|
disableHeaderCellsStyleMap = new HashMap<>(); |
|
tempHeaderCells = new ArrayList<>(); |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error(e, e.getMessage()); |
|
} |
|
} |
|
|
|
} |
|
} |
|
|
|
|
|
protected void refresh() { |
|
validate(); |
|
repaint(); |
|
revalidate(); |
|
} |
|
} |