package org.nightlabs.jfire.issuetracking.ui.issue.editor; import java.text.DateFormat; import java.util.List; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.MenuItem; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.forms.editor.FormPage; import org.eclipse.ui.forms.events.ExpansionAdapter; import org.eclipse.ui.forms.events.ExpansionEvent; import org.eclipse.ui.forms.widgets.ExpandableComposite; import org.eclipse.ui.forms.widgets.FormToolkit; import org.nightlabs.base.ui.composite.XComposite; import org.nightlabs.base.ui.composite.XComposite.LayoutMode; import org.nightlabs.jfire.issue.Issue; import org.nightlabs.jfire.issue.IssueComment; import org.nightlabs.jfire.issuetracking.ui.resource.Messages; /** * @author Chairat Kongarayawetchakun <!-- chairat [AT] nightlabs [DOT] de --> * */ public class IssueCommentListSection extends AbstractIssueEditorGeneralSection { private Issue issue; private FormToolkit toolkit; private XComposite commentComposite; private int oldSize; public IssueCommentListSection(FormPage page, Composite parent, final IssueEditorPageController controller) { super(page, parent, controller); getSection().setText(Messages.getString("org.nightlabs.jfire.issuetracking.ui.issue.editor.IssueCommentListSection.section.text")); //$NON-NLS-1$ getSection().setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // Sets up the toolkit. toolkit = new FormToolkit(getSection().getShell().getDisplay()); commentComposite = new XComposite(getSection(), SWT.NONE, LayoutMode.TIGHT_WRAPPER); commentComposite.getGridLayout().numColumns = 1; getSection().setClient(commentComposite); } private boolean firstLoaded = true; @Override protected void doSetIssue(Issue newIssue) { if (issue != null && newIssue.getComments().size() == oldSize) { return; } issue = newIssue; oldSize = issue.getComments().size(); getSection().setText(String.format( Messages.getString("org.nightlabs.jfire.issuetracking.ui.issue.editor.IssueCommentListSection.section.fillNo.text"), //$NON-NLS-1$ oldSize) ); if (firstLoaded) { List<IssueComment> comments = newIssue.getComments(); for (int i = 0; i < comments.size(); i++) { addComment(commentComposite, comments.get(i), i == comments.size() - 1 ? true : false); } firstLoaded = false; } else { if (newIssue.getComments().size() > 0) { addComment(commentComposite, newIssue.getComments().get(newIssue.getComments().size() - 1), true); } } reflowParentForm(); } protected void reflowParentForm() { getManagedForm().getForm().getBody().layout(true, true); getManagedForm().getForm().reflow(true); } private static DateFormat dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); private void addComment(Composite parent, IssueComment comment, boolean expand) { ExpandableComposite commentEntry = new ExpandableComposite( parent, SWT.NONE, ExpandableComposite.COMPACT | ExpandableComposite.TREE_NODE | ExpandableComposite.EXPANDED); commentEntry.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); commentEntry.setFont(new Font(getSection().getDisplay(), new FontData("Courier", 10, SWT.BOLD))); //$NON-NLS-1$ commentEntry.setText(String.format("%s - %s", //$NON-NLS-1$ comment.getUser().getName(), dateTimeFormat.format(comment.getCreateTimestamp()))); toolkit.adapt(commentEntry); /********Using Text********/ Text text = toolkit.createText(commentEntry, comment.getText(), SWT.MULTI | SWT.WRAP); text.setEditable(false); text.setFont(new Font(getSection().getDisplay(), new FontData("Courier", 10, SWT.NORMAL))); //$NON-NLS-1$ commentEntry.setClient(text); // /********Using FormText********/ // FormText text = toolkit.createFormText(commentEntry, false); // text.setText(comment.getText(), // false, // false); // text.setWhitespaceNormalized(false); // commentEntry.setClient(text); commentEntry.addExpansionListener(new ExpansionAdapter() { @Override public void expansionStateChanged(ExpansionEvent e) { // resizes the application window. reflowParentForm(); } }); commentEntry.setExpanded(expand); //Adds actions to each comment, Chairat Menu bar = new Menu(commentEntry); commentEntry.setMenu(bar); MenuItem fileMenu = new MenuItem(bar, SWT.CASCADE); fileMenu.setText("&File"); } }