下面列出了怎么用javafx.scene.Node的API类实例代码及写法,或者点击链接到github查看源代码。
private ObservableList<Node> getChildren(Node node) {
if (node instanceof ButtonBar) {
return ((ButtonBar) node).getButtons();
}
if (node instanceof ToolBar) {
return ((ToolBar) node).getItems();
}
if (node instanceof Pane) {
return ((Pane) node).getChildren();
}
if (node instanceof TabPane) {
ObservableList<Node> contents = FXCollections.observableArrayList();
ObservableList<Tab> tabs = ((TabPane) node).getTabs();
for (Tab tab : tabs) {
contents.add(tab.getContent());
}
return contents;
}
return FXCollections.observableArrayList();
}
@Override
protected void updateItem(File item, boolean empty) {
super.updateItem(item, empty);
// add/remove separator below "open folder" item
if (!empty && item == OPEN_FOLDER && ProjectsComboBox.this.getItems().size() > 1)
getStyleClass().add("open-project");
else
getStyleClass().remove("open-project");
String text = null;
Node graphic = null;
if (!empty && item != null) {
text = (item == OPEN_FOLDER)
? Messages.get("ProjectsComboBox.openProject")
: item.getAbsolutePath();
graphic = closeButton;
closeButton.setVisible(item != OPEN_FOLDER);
}
setText(text);
setGraphic(graphic);
}
static <T> void startEdit(final Cell<T> cell,
final StringConverter<T> converter,
final HBox hbox,
final Node graphic,
final TextField textField) {
if (textField != null) {
textField.setText(getItemText(cell, converter));
}
cell.setText(null);
if (graphic != null) {
hbox.getChildren().setAll(graphic, textField);
cell.setGraphic(hbox);
} else {
cell.setGraphic(textField);
}
textField.selectAll();
// requesting focus so that key input can immediately go into the
// TextField (see RT-28132)
textField.requestFocus();
}
public DashboardTile(String title, String description, Node graphic) {
getStyleClass().addAll("dashboard-modules-tile");
Label titleLabel = new Label(title);
titleLabel.getStyleClass().add("dashboard-modules-tile-title");
if (nonNull(graphic)) {
titleLabel.setGraphic(graphic);
}
Label textLabel = new Label(description);
textLabel.getStyleClass().add("dashboard-modules-tile-text");
textLabel.setMinHeight(USE_PREF_SIZE);
VBox topTile = new VBox(5);
topTile.getChildren().addAll(titleLabel, textLabel);
button.getStyleClass().add("dashboard-modules-invisible-button");
button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
armed.bind(button.armedProperty());
getChildren().addAll(new StackPane(topTile, button));
setMaxHeight(USE_PREF_SIZE);
setMinHeight(USE_PREF_SIZE);
}
public MainApplicationController(Stage mainStage,
DataModel model,
Application fxApplication,
ApplicationSettings appSettings,
Node loggingPaneArea,
ControllerRepositoryFactory controllersRepositoryFactory,
DefaultActionHandlerFactory actionHandlerFactory,
ApplicationBusySwitcher busySwitcher) {
appStage = mainStage;
dataModel = model;
this.fxApplication = fxApplication;
this.appSettings = appSettings;
this.loggingPaneArea = loggingPaneArea;
this.controllersRepositoryFactory = controllersRepositoryFactory;
this.actionHandlerFactory = actionHandlerFactory;
this.busySwitcher = busySwitcher;
}
protected Tab newTab(Node nd)
{
Node n = DockTools.prepareToAdd(nd);
Tab t = new Tab(null, n);
if(n instanceof FxDockPane)
{
FxDockPane p = (FxDockPane)n;
t.setGraphic(p.titleField);
t.setOnClosed((ev) ->
{
Node pp = DockTools.getParent(this);
DockTools.collapseEmptySpace(pp);
});
}
return t;
}
private void submitLogEntry(final Node parent, final String title, final String body, final File image_file)
{
LogEntryBuilder logEntryBuilder = new LogEntryBuilder();
if (title != null)
logEntryBuilder.title(title);
if (body != null)
logEntryBuilder.appendDescription(body);
if (image_file != null)
{
try
{
final Attachment attachment = AttachmentImpl.of(image_file, "image", false);
logEntryBuilder.attach(attachment);
}
catch (FileNotFoundException ex)
{
logger.log(Level.WARNING, "Cannot attach " + image_file, ex);
}
}
final LogEntryModel model = new LogEntryModel(logEntryBuilder.createdDate(Instant.now()).build());
new LogEntryEditorStage(parent, model, null).show();
}
default Node createGui(Stage stage, T value, Consumer<T> onAction) {
TextField valueField = new TextField(toString(value));
Runnable updateValue = () -> {
String newValue = valueField.getText();
if(!newValue.equals(value)) {
try {
onAction.accept(parse(newValue));
} catch(Exception exc) {
exc.printStackTrace();
valueField.setText(toString(value));
}
}
};
valueField.setOnAction(event -> updateValue.run());
valueField.focusedProperty().addListener((observable, oldValue, newValue) -> {
if(!newValue) {
updateValue.run();
}
});
return valueField;
}
private static Object getConstraint(Node node, Object key) {
if (node.hasProperties()) {
Object value = node.getProperties().get(key);
if (value != null) {
return value;
}
}
return null;
}
private Node createErrorNode() {
Polyline errorNode = new Polyline(
0, 0, ERROR_SIZE, 0, ERROR_SIZE, ERROR_SIZE, 0, ERROR_SIZE, 0, 0, // rectangle
ERROR_SIZE, ERROR_SIZE, 0, ERROR_SIZE, ERROR_SIZE, 0); // cross
errorNode.setStroke(Color.RED); //TODO use CSS
return errorNode;
}
/**
* Activate the selected painting component.
*
* @param observable the component box's property.
* @param oldValue the previous component.
* @param newValue the new component.
*/
@FxThread
private void activate(
@NotNull ObservableValue<? extends PaintingComponent> observable,
@Nullable PaintingComponent oldValue,
@Nullable PaintingComponent newValue
) {
var items = getContainer().getChildren();
if (oldValue != null) {
oldValue.notifyHided();
oldValue.stopPainting();
items.remove(oldValue);
}
var paintedObject = getPaintedObject();
if (newValue != null) {
if (paintedObject != null) {
newValue.startPainting(paintedObject);
}
if (isShowed()) {
newValue.notifyShowed();
}
items.add((Node) newValue);
}
setCurrentComponent(newValue);
}
public Node getIcon(final Path path) {
FontIcon fontIcon = new FontIcon(FontAwesome.FILE_O);
if (Files.isDirectory(path)) {
fontIcon.setIconCode(FontAwesome.FOLDER_O);
} else {
if (pathResolver.isAsciidoc(path) || pathResolver.isMarkdown(path))
fontIcon.setIconCode(FontAwesome.FILE_TEXT_O);
if (pathResolver.isXML(path) || pathResolver.isCode(path))
fontIcon.setIconCode(FontAwesome.FILE_CODE_O);
if (pathResolver.isImage(path))
fontIcon.setIconCode(FontAwesome.FILE_PICTURE_O);
if (pathResolver.isPDF(path))
fontIcon.setIconCode(FontAwesome.FILE_PDF_O);
if (pathResolver.isHTML(path))
fontIcon.setIconCode(FontAwesome.HTML5);
if (pathResolver.isArchive(path))
fontIcon.setIconCode(FontAwesome.FILE_ZIP_O);
if (pathResolver.isExcel(path))
fontIcon.setIconCode(FontAwesome.FILE_EXCEL_O);
if (pathResolver.isVideo(path))
fontIcon.setIconCode(FontAwesome.FILE_VIDEO_O);
if (pathResolver.isWord(path))
fontIcon.setIconCode(FontAwesome.FILE_WORD_O);
if (pathResolver.isPPT(path))
fontIcon.setIconCode(FontAwesome.FILE_POWERPOINT_O);
if (pathResolver.isBash(path))
fontIcon.setIconCode(FontAwesome.TERMINAL);
}
return new Label(null, fontIcon);
}
/**
* 注釈プラグインの初期化
*/
private void initializeRemarkPlugin() {
for (Updateable<DeckPort> plugin : Plugin.getContent(FleetTabRemark.class)) {
Node node;
if (plugin instanceof Node) {
node = ((Node) plugin);
} else {
node = new RemarkLabel(plugin);
}
this.remark.getChildren().add(node);
}
}
@Override
public void onChanged(
javafx.collections.MapChangeListener.Change<? extends Node, ? extends IVisualPart<? extends Node>> change) {
// keep model in sync with part hierarchy
if (change.wasRemoved()) {
IVisualPart<? extends Node> valueRemoved = change
.getValueRemoved();
if (selection.contains(valueRemoved)) {
selection.remove(valueRemoved);
}
}
}
@SuppressWarnings("rawtypes")
public static void disableIfNotSelected(SelectionModel selectionModel, Node... nodes) {
BooleanBinding selected = selectionModel.selectedItemProperty().isNull();
for (Node node : nodes) {
node.disableProperty().bind(selected);
}
}
@Override
public Animation createShowingAnimation(Node contentContainer, Node overlay) {
return new CachedTransition(contentContainer, new Timeline(
new KeyFrame(Duration.millis(1000),
new KeyValue(contentContainer.scaleXProperty(), 1, Interpolator.EASE_OUT),
new KeyValue(contentContainer.scaleYProperty(), 1, Interpolator.EASE_OUT),
new KeyValue(overlay.opacityProperty(), 1, Interpolator.EASE_BOTH)
))) {
{
setCycleDuration(Duration.millis(160));
setDelay(Duration.seconds(0));
}
};
}
private boolean updateDisplayText(ListCell<T> cell, T item, boolean empty) {
if (empty) {
// create empty cell
if (cell == null) {
return true;
}
cell.setGraphic(null);
cell.setText(null);
return true;
} else if (item instanceof Node) {
Node currentNode = cell.getGraphic();
Node newNode = (Node) item;
// create a node from the selected node of the listview
// using JFXComboBox {@link #nodeConverterProperty() NodeConverter})
NodeConverter<T> nc = this.getNodeConverter();
Node node = nc == null ? null : nc.toNode(item);
if (currentNode == null || !currentNode.equals(newNode)) {
cell.setText(null);
cell.setGraphic(node == null ? newNode : node);
}
return node == null;
} else {
// run item through StringConverter if it isn't null
StringConverter<T> c = this.getConverter();
String s = item == null ? this.getPromptText() : (c == null ? item.toString() : c.toString(item));
cell.setText(s);
cell.setGraphic(null);
return s == null || s.isEmpty();
}
}
private void setIconColorOnChildren(Paint color) {
for (Node node : getChildren()) {
if (node instanceof Icon) {
((Icon) node).setIconColor(color);
}
}
}
public static <T, N extends Node,C extends Cell<T, N>>
CellWrapper<T, N, C> beforeUpdateIndex(C cell, IntConsumer action) {
return new CellWrapper<T, N, C>(cell) {
@Override
public void updateIndex(int index) {
action.accept(index);
super.updateIndex(index);
}
};
}
private void install(Node node, Tooltip tooltip) {
if (tooltip == null) {
return;
}
if (tooltip instanceof JFXTooltip) {
JFXTooltip.install(node, (JFXTooltip) tooltip);
} else {
Tooltip.install(node, tooltip);
}
}
@Override
public void onChanged(
javafx.collections.MapChangeListener.Change<? extends Node, ? extends IVisualPart<? extends Node>> change) {
// keep model in sync with part hierarchy
if (change.wasRemoved()) {
if (focusedProperty.get() == change.getValueRemoved()) {
setFocus(null);
}
}
}
public static Node createIconContent() {
ImageView iv = new ImageView(BOAT);
iv.setFitWidth(80);
iv.setFitHeight(80);
iv.setViewport(new Rectangle2D(90,0,332,332));
final SepiaTone SepiaTone = new SepiaTone();
SepiaTone.setLevel(1);
iv.setEffect(SepiaTone);
return iv;
}
@Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
AbstractAttributeInteraction<?> interaction = AbstractAttributeInteraction.getInteraction(attrDataType);
final String displayText;
final List<Node> displayNodes;
if (item == null) {
displayText = NO_VALUE_TEXT;
displayNodes = Collections.emptyList();
} else {
displayText = interaction.getDisplayText(item);
displayNodes = interaction.getDisplayNodes(item, -1, CELL_HEIGHT - 1);
}
GridPane gridPane = new GridPane();
gridPane.setHgap(CELL_ITEM_SPACING);
ColumnConstraints displayNodeConstraint = new ColumnConstraints(CELL_HEIGHT - 1);
displayNodeConstraint.setHalignment(HPos.CENTER);
for (int i = 0; i < displayNodes.size(); i++) {
final Node displayNode = displayNodes.get(i);
gridPane.add(displayNode, i, 0);
gridPane.getColumnConstraints().add(displayNodeConstraint);
}
setGraphic(gridPane);
setPrefHeight(CELL_HEIGHT);
setText(displayText);
}
/**
*
* @param viewer
* The {@link IViewer}.
* @param hoverIntent
* The hover intent {@link Node}.
*/
protected void notifyHoverIntent(IViewer viewer, Node hoverIntent) {
// determine hover policies
Collection<? extends IOnHoverHandler> policies = getHandlerResolver()
.resolve(HoverGesture.this, hoverIntent, viewer,
ON_HOVER_POLICY_KEY);
getDomain().openExecutionTransaction(HoverGesture.this);
// active policies are unnecessary because hover is not a
// gesture, just one event at one point in time
for (IOnHoverHandler policy : policies) {
policy.hoverIntent(hoverIntent);
}
getDomain().closeExecutionTransaction(HoverGesture.this);
}
@Override
protected void seriesRemoved(final Series<X, Y> series) {
if (labelType != null && labelType != LabelType.NotDisplay && labelType != LabelType.Pop) {
for (Node bar : nodeMap.keySet()) {
Node text = nodeMap.get(bar);
this.getPlotChildren().remove(text);
}
nodeMap.clear();
}
super.seriesRemoved(series);
}
public static Node createIconContent() {
StackPane sp = new StackPane();
Rectangle rectangle = new Rectangle(62, 62, Color.LIGHTGREY);
rectangle.setStroke(Color.BLACK);
sp.setPrefSize(rectangle.getWidth(), rectangle.getHeight());
Rectangle biggerRec = new Rectangle(55, 55, Color.web("#1c89f4"));
Rectangle smallerRec = new Rectangle(35, 35, Color.web("#349b00"));
sp.getChildren().addAll(rectangle, biggerRec, smallerRec);
return new Group(sp);
}
/**
* Gets the closest ancestor (e.g. parent, grandparent) to a node that is a subclass of {@link Region}.
*
* @param node a JavaFX {@link Node}
* @return the node's closest ancestor that is a subclass of {@link Region}, or {@code null} if none exists
*/
private Region getContainer(final Node node) {
final Parent parent = node.getParent();
if (parent == null) {
return null;
} else if (parent instanceof Region) {
return (Region) parent;
} else {
return getContainer(parent);
}
}
private void setActionHandlers(Node node) {
currentIcon.setManaged(true);
currentIcon.setVisible(true);
// As we don't use binding here we need to recreate it on mouse over to reflect the current state
currentIcon.setOnMouseEntered(e -> popoverWrapper.showPopOver(() -> createPopOver(node)));
currentIcon.setOnMouseExited(e -> popoverWrapper.hidePopOver());
}
public static Node getRefIcon(GitRef ref) {
if (ref instanceof GitTag) {
return getIcon(Octicons.Glyph.TAG);
} else if (ref instanceof GitBranch) {
return getIcon(Octicons.Glyph.BRANCH);
} else {
return null;
}
}
@Override
public void remove(Node container) {
container.getProperties().remove(DockingDesktop.DOCKING_CONTAINER);
getItems().remove(container);
if (getItems().size() == 0) {
((IDockingContainer) getProperties().get(DockingDesktop.DOCKING_CONTAINER)).remove(this);
}
}