-
Notifications
You must be signed in to change notification settings - Fork 843
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6401 from moacirrf/ft-markdown-checkbox
Checkbox support on Markdown preview
- Loading branch information
Showing
9 changed files
with
167 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
ide/markdown/src/org/netbeans/modules/markdown/ui/preview/MarkdownEditorKit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.netbeans.modules.markdown.ui.preview; | ||
|
||
import org.netbeans.modules.markdown.ui.preview.views.MarkdownViewFactory; | ||
import javax.swing.text.ViewFactory; | ||
import javax.swing.text.html.HTMLEditorKit; | ||
|
||
/** | ||
* | ||
* @author moacirrf | ||
*/ | ||
public class MarkdownEditorKit extends HTMLEditorKit { | ||
|
||
private final transient ViewFactory viewFactory; | ||
|
||
public MarkdownEditorKit() { | ||
super(); | ||
this.viewFactory = new MarkdownViewFactory(); | ||
} | ||
|
||
@Override | ||
public ViewFactory getViewFactory() { | ||
return viewFactory; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
ide/markdown/src/org/netbeans/modules/markdown/ui/preview/views/CheckboxView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.netbeans.modules.markdown.ui.preview.views; | ||
|
||
import java.awt.Component; | ||
import java.awt.event.ActionEvent; | ||
import javax.swing.BorderFactory; | ||
import javax.swing.JCheckBox; | ||
import javax.swing.text.Element; | ||
import javax.swing.text.html.FormView; | ||
|
||
/** | ||
* | ||
* @author moacirrf | ||
*/ | ||
public class CheckboxView extends FormView { | ||
|
||
public CheckboxView(Element elem) { | ||
super(elem); | ||
} | ||
|
||
@Override | ||
protected Component createComponent() { | ||
Component component = super.createComponent(); | ||
if (component instanceof JCheckBox) { | ||
JCheckBox c = (JCheckBox) component; | ||
c.setBorder(BorderFactory.createEmptyBorder(0, 0, -4, 0)); | ||
c.addActionListener((ActionEvent e) -> c.setSelected(!c.isSelected())); | ||
} | ||
return component; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
ide/markdown/src/org/netbeans/modules/markdown/ui/preview/views/MarkdownViewFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.netbeans.modules.markdown.ui.preview.views; | ||
|
||
import javax.swing.text.AbstractDocument; | ||
import javax.swing.text.AttributeSet; | ||
import javax.swing.text.Element; | ||
import javax.swing.text.StyleConstants; | ||
import javax.swing.text.View; | ||
import javax.swing.text.html.HTML; | ||
import javax.swing.text.html.HTMLEditorKit; | ||
|
||
/** | ||
* This class creates Views for a JEditorPane, it receives and HTML element and | ||
* you must create the most similar swing component to this html element. For | ||
* example a HTML input type checkbox, its a JCheckBox, and image can be a | ||
* JLabel and so on. | ||
* | ||
* @author moacirrf | ||
*/ | ||
public class MarkdownViewFactory extends HTMLEditorKit.HTMLFactory { | ||
|
||
@Override | ||
public View create(Element elem) { | ||
|
||
if (isElementOfTag(elem, HTML.Tag.INPUT)) { | ||
return new CheckboxView(elem); | ||
} | ||
|
||
return super.create(elem); | ||
} | ||
|
||
public boolean isElementOfTag(Element elem, HTML.Tag tag) { | ||
|
||
AttributeSet attrs = elem.getAttributes(); | ||
Object elementName | ||
= attrs.getAttribute(AbstractDocument.ElementNameAttribute); | ||
Object o = (elementName != null) | ||
? null : attrs.getAttribute(StyleConstants.NameAttribute); | ||
HTML.Tag kind = (HTML.Tag) o; | ||
|
||
return (o instanceof HTML.Tag) && (kind == tag); | ||
|
||
} | ||
} |